Skip to content

Commit acce8be

Browse files
committed
feat: implement badgerdb database with basic CRUD operations
Signed-off-by: tbxark <[email protected]>
1 parent 8e36fe0 commit acce8be

File tree

3 files changed

+200
-25
lines changed

3 files changed

+200
-25
lines changed

cache/badgerdb/database.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package badgerdb
2+
3+
import (
4+
"context"
5+
"github.com/dgraph-io/badger/v4"
6+
"time"
7+
)
8+
9+
type Config struct {
10+
Path string `json:"path"`
11+
}
12+
13+
type Database struct {
14+
db *badger.DB
15+
}
16+
17+
func NewDatabase(config *Config) (*Database, error) {
18+
db, err := badger.Open(badger.DefaultOptions("/tmp/badger"))
19+
if err != nil {
20+
return nil, err
21+
}
22+
return &Database{
23+
db: db,
24+
}, nil
25+
}
26+
27+
func (d *Database) Set(ctx context.Context, key string, val []byte, expiration time.Duration) error {
28+
return d.db.Update(func(txn *badger.Txn) error {
29+
return txn.Set([]byte(key), val)
30+
})
31+
}
32+
33+
func (d *Database) Get(ctx context.Context, key string) (*[]byte, error) {
34+
var val []byte
35+
err := d.db.View(func(txn *badger.Txn) error {
36+
item, err := txn.Get([]byte(key))
37+
if err != nil {
38+
return err
39+
}
40+
val, err = item.ValueCopy(nil)
41+
if err != nil {
42+
return err
43+
}
44+
return nil
45+
})
46+
if err != nil {
47+
return nil, err
48+
}
49+
return &val, nil
50+
}
51+
52+
func (d *Database) Del(ctx context.Context, key string) error {
53+
return d.db.Update(func(txn *badger.Txn) error {
54+
return txn.Delete([]byte(key))
55+
})
56+
}
57+
58+
func (d *Database) MultiSet(ctx context.Context, valMap map[string][]byte, expiration time.Duration) error {
59+
return d.db.Update(func(txn *badger.Txn) error {
60+
for k, v := range valMap {
61+
err := txn.SetEntry(badger.NewEntry([]byte(k), v).WithTTL(expiration))
62+
if err != nil {
63+
return err
64+
}
65+
}
66+
return nil
67+
})
68+
}
69+
70+
func (d *Database) MultiGet(ctx context.Context, keys []string) (map[string][]byte, error) {
71+
res := make(map[string][]byte)
72+
err := d.db.View(func(txn *badger.Txn) error {
73+
for _, key := range keys {
74+
item, err := txn.Get([]byte(key))
75+
if err != nil {
76+
return err
77+
}
78+
val, err := item.ValueCopy(nil)
79+
if err != nil {
80+
return err
81+
}
82+
res[key] = val
83+
}
84+
return nil
85+
})
86+
if err != nil {
87+
return nil, err
88+
}
89+
return res, nil
90+
}
91+
92+
func (d *Database) MultiDel(ctx context.Context, keys []string) error {
93+
return d.db.Update(func(txn *badger.Txn) error {
94+
for _, key := range keys {
95+
err := d.Del(ctx, key)
96+
if err != nil {
97+
return err
98+
}
99+
}
100+
return nil
101+
})
102+
}
103+
104+
func (d *Database) DelAll(ctx context.Context) error {
105+
return d.db.DropAll()
106+
}
107+
108+
func (d *Database) Close() error {
109+
return d.db.Close()
110+
}

go.mod

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.23.2
55
require (
66
entgo.io/ent v0.14.2
77
github.com/TBXark/jsoncompressor v0.0.0-20250220090015-07c767467512
8+
github.com/dgraph-io/badger/v4 v4.5.1
89
github.com/gin-contrib/cors v1.7.3
910
github.com/gin-contrib/pprof v1.5.2
1011
github.com/gin-contrib/zap v1.1.4
@@ -25,6 +26,7 @@ require (
2526
golang.org/x/crypto v0.33.0
2627
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa
2728
golang.org/x/sync v0.11.0
29+
golang.org/x/text v0.22.0
2830
golang.org/x/time v0.10.0
2931
gopkg.in/natefinch/lumberjack.v2 v2.2.1
3032
modernc.org/sqlite v1.35.0
@@ -40,6 +42,7 @@ require (
4042
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4143
github.com/cloudwego/base64x v0.1.5 // indirect
4244
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
45+
github.com/dgraph-io/ristretto/v2 v2.1.0 // indirect
4346
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
4447
github.com/dustin/go-humanize v1.0.1 // indirect
4548
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
@@ -54,10 +57,11 @@ require (
5457
github.com/go-playground/locales v0.14.1 // indirect
5558
github.com/go-playground/universal-translator v0.18.1 // indirect
5659
github.com/go-playground/validator/v10 v10.25.0 // indirect
57-
github.com/go-redis/redis/v8 v8.11.5 // indirect
5860
github.com/goccy/go-json v0.10.5 // indirect
5961
github.com/gofrs/flock v0.12.1 // indirect
6062
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
63+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
64+
github.com/google/flatbuffers v24.12.23+incompatible // indirect
6165
github.com/google/uuid v1.6.0 // indirect
6266
github.com/josharian/intern v1.0.0 // indirect
6367
github.com/json-iterator/go v1.1.12 // indirect
@@ -72,16 +76,17 @@ require (
7276
github.com/modern-go/reflect2 v1.0.2 // indirect
7377
github.com/ncruces/go-strftime v0.1.9 // indirect
7478
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
79+
github.com/pkg/errors v0.9.1 // indirect
7580
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
7681
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
7782
github.com/rs/xid v1.6.0 // indirect
7883
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
7984
github.com/ugorji/go/codec v1.2.12 // indirect
85+
go.opencensus.io v0.24.0 // indirect
8086
go.uber.org/multierr v1.11.0 // indirect
8187
golang.org/x/arch v0.14.0 // indirect
8288
golang.org/x/net v0.35.0 // indirect
8389
golang.org/x/sys v0.30.0 // indirect
84-
golang.org/x/text v0.22.0 // indirect
8590
golang.org/x/tools v0.30.0 // indirect
8691
google.golang.org/protobuf v1.36.5 // indirect
8792
gopkg.in/yaml.v3 v3.0.1 // indirect

0 commit comments

Comments
 (0)