Skip to content

Commit b725d8d

Browse files
authored
feat(badger): bump to v4 and reset on reload (#36)
* feat(badger): bump to v4 and reset on reload * bump(version): to v0.0.16
1 parent e555faf commit b725d8d

File tree

36 files changed

+308
-430
lines changed

36 files changed

+308
-430
lines changed

.github/workflows/static.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
with:
6161
go-version: ${{ env.GO_VERSION }}
6262
- name: golangci-lint
63-
uses: golangci/golangci-lint-action@v3
63+
uses: golangci/golangci-lint-action@v8
6464
with:
6565
working-directory: ${{ matrix.submodules }}
6666
- name: Install olric

.golangci.yml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
run:
2-
go: '1.22.2'
3-
4-
output:
5-
formats:
6-
- format: json
7-
path: stderr
8-
- format: checkstyle
9-
path: report.xml
10-
- format: colored-line-number
1+
version: "2"
112

123
linters:
13-
enable-all: true
4+
default: all
5+
settings:
6+
wsl_v5:
7+
allow-first-in-block: true
8+
allow-whole-block: false
9+
branch-max-lines: 2
1410
disable:
1511
- bodyclose
1612
- containedctx
@@ -23,14 +19,16 @@ linters:
2319
- gochecknoglobals
2420
- gocognit
2521
- gomoddirectives
26-
- gomnd
22+
# - gomnd
2723
- interfacebloat
2824
- ireturn
2925
- lll
3026
- mnd
3127
- nestif
28+
- noinlineerr
3229
- nonamedreturns
3330
- paralleltest
34-
- stylecheck
31+
# - stylecheck
3532
- revive
36-
- wrapcheck
33+
- wrapcheck
34+
- wsl

badger/badger.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import (
1515

1616
"dario.cat/mergo"
1717
"github.com/darkweak/storages/core"
18-
"github.com/dgraph-io/badger/v3"
18+
"github.com/dgraph-io/badger/v4"
1919
"github.com/pierrec/lz4/v4"
2020
"go.uber.org/zap"
2121
)
2222

2323
// Badger provider type.
2424
type Badger struct {
2525
*badger.DB
26+
2627
stale time.Duration
2728
logger core.Logger
2829
}
@@ -37,7 +38,7 @@ type badgerLogger struct {
3738
}
3839

3940
func (b *badgerLogger) Warningf(msg string, params ...interface{}) {
40-
b.SugaredLogger.Warnf(msg, params...)
41+
b.Warnf(msg, params...)
4142
}
4243

4344
// Factory function create new Badger instance.
@@ -48,6 +49,7 @@ func Factory(badgerConfiguration core.CacheProvider, logger core.Logger, stale t
4849

4950
if badgerConfiguration.Configuration != nil {
5051
var parsedBadger badger.Options
52+
5153
if b, e := json.Marshal(badgerConfiguration.Configuration); e == nil {
5254
if e = json.Unmarshal(b, &parsedBadger); e != nil {
5355
logger.Error("Impossible to parse the configuration for the default provider (Badger)", e)
@@ -112,7 +114,7 @@ func (provider *Badger) Uuid() string {
112114
func (provider *Badger) MapKeys(prefix string) map[string]string {
113115
keys := map[string]string{}
114116

115-
_ = provider.DB.View(func(txn *badger.Txn) error {
117+
_ = provider.View(func(txn *badger.Txn) error {
116118
opts := badger.DefaultIteratorOptions
117119
opts.PrefetchValues = false
118120
iterator := txn.NewIterator(opts)
@@ -139,7 +141,7 @@ func (provider *Badger) MapKeys(prefix string) map[string]string {
139141
func (provider *Badger) ListKeys() []string {
140142
keys := []string{}
141143

142-
err := provider.DB.View(func(txn *badger.Txn) error {
144+
err := provider.View(func(txn *badger.Txn) error {
143145
opts := badger.DefaultIteratorOptions
144146
opts.PrefetchValues = false
145147
it := txn.NewIterator(opts)
@@ -174,7 +176,7 @@ func (provider *Badger) Get(key string) []byte {
174176

175177
var result []byte
176178

177-
err := provider.DB.View(func(txn *badger.Txn) error {
179+
err := provider.View(func(txn *badger.Txn) error {
178180
i, err := txn.Get([]byte(key))
179181
item = i
180182

@@ -198,7 +200,7 @@ func (provider *Badger) Get(key string) []byte {
198200

199201
// GetMultiLevel tries to load the key and check if one of linked keys is a fresh/stale candidate.
200202
func (provider *Badger) GetMultiLevel(key string, req *http.Request, validator *core.Revalidator) (fresh *http.Response, stale *http.Response) {
201-
_ = provider.DB.View(func(tx *badger.Txn) error {
203+
_ = provider.View(func(tx *badger.Txn) error {
202204
result, err := tx.Get([]byte(core.MappingKeyPrefix + key))
203205
if err != nil && !errors.Is(err, badger.ErrKeyNotFound) {
204206
return err
@@ -226,7 +228,7 @@ func (provider *Badger) GetMultiLevel(key string, req *http.Request, validator *
226228
func (provider *Badger) SetMultiLevel(baseKey, variedKey string, value []byte, variedHeaders http.Header, etag string, duration time.Duration, realKey string) error {
227229
now := time.Now()
228230

229-
err := provider.DB.Update(func(btx *badger.Txn) error {
231+
err := provider.Update(func(btx *badger.Txn) error {
230232
var err error
231233

232234
compressed := new(bytes.Buffer)
@@ -280,7 +282,7 @@ func (provider *Badger) SetMultiLevel(baseKey, variedKey string, value []byte, v
280282

281283
// Set method will store the response in Badger provider.
282284
func (provider *Badger) Set(key string, value []byte, duration time.Duration) error {
283-
err := provider.DB.Update(func(txn *badger.Txn) error {
285+
err := provider.Update(func(txn *badger.Txn) error {
284286
return txn.SetEntry(badger.NewEntry([]byte(key), value).WithTTL(duration))
285287
})
286288
if err != nil {
@@ -292,20 +294,19 @@ func (provider *Badger) Set(key string, value []byte, duration time.Duration) er
292294

293295
// Delete method will delete the response in Badger provider if exists corresponding to key param.
294296
func (provider *Badger) Delete(key string) {
295-
_ = provider.DB.Update(func(txn *badger.Txn) error {
297+
_ = provider.Update(func(txn *badger.Txn) error {
296298
return txn.Delete([]byte(key))
297299
})
298300
}
299301

300302
// DeleteMany method will delete the responses in Badger provider if exists corresponding to the regex key param.
301303
func (provider *Badger) DeleteMany(key string) {
302304
rgKey, e := regexp.Compile(key)
303-
304305
if e != nil {
305306
return
306307
}
307308

308-
_ = provider.DB.View(func(txn *badger.Txn) error {
309+
_ = provider.View(func(txn *badger.Txn) error {
309310
opts := badger.DefaultIteratorOptions
310311
opts.PrefetchValues = false
311312
it := txn.NewIterator(opts)
@@ -330,5 +331,13 @@ func (provider *Badger) Init() error {
330331

331332
// Reset method will reset or close provider.
332333
func (provider *Badger) Reset() error {
333-
return provider.DB.DropAll()
334+
if err := provider.DropAll(); err != nil {
335+
provider.logger.Errorf("Impossible to reset the Badger DB, %v", err)
336+
}
337+
338+
if err := provider.Close(); err != nil {
339+
provider.logger.Errorf("Impossible to close the Badger DB, %v", err)
340+
}
341+
342+
return nil
334343
}

badger/caddy/go.mod

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module github.com/darkweak/storages/badger/caddy
22

3-
go 1.22.1
3+
go 1.23.0
44

55
require (
66
github.com/caddyserver/caddy/v2 v2.8.4
7-
github.com/darkweak/storages/badger v0.0.15
8-
github.com/darkweak/storages/core v0.0.15
7+
github.com/darkweak/storages/badger v0.0.16
8+
github.com/darkweak/storages/core v0.0.16
99
)
1010

1111
require (
@@ -22,28 +22,29 @@ require (
2222
github.com/caddyserver/certmagic v0.21.3 // indirect
2323
github.com/caddyserver/zerossl v0.1.3 // indirect
2424
github.com/cespare/xxhash v1.1.0 // indirect
25-
github.com/cespare/xxhash/v2 v2.2.0 // indirect
25+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2626
github.com/chzyer/readline v1.5.1 // indirect
27-
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
27+
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
2828
github.com/dgraph-io/badger v1.6.2 // indirect
2929
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
30-
github.com/dgraph-io/badger/v3 v3.2103.5 // indirect
30+
github.com/dgraph-io/badger/v4 v4.8.0 // indirect
3131
github.com/dgraph-io/ristretto v0.1.1 // indirect
32-
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
32+
github.com/dgraph-io/ristretto/v2 v2.2.0 // indirect
33+
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
3334
github.com/dustin/go-humanize v1.0.1 // indirect
3435
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
3536
github.com/go-kit/kit v0.13.0 // indirect
3637
github.com/go-kit/log v0.2.1 // indirect
3738
github.com/go-logfmt/logfmt v0.6.0 // indirect
39+
github.com/go-logr/logr v1.4.3 // indirect
40+
github.com/go-logr/stdr v1.2.2 // indirect
3841
github.com/go-sql-driver/mysql v1.7.1 // indirect
3942
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
40-
github.com/gogo/protobuf v1.3.2 // indirect
4143
github.com/golang/glog v1.2.0 // indirect
42-
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
4344
github.com/golang/protobuf v1.5.4 // indirect
4445
github.com/golang/snappy v0.0.4 // indirect
4546
github.com/google/cel-go v0.20.1 // indirect
46-
github.com/google/flatbuffers v23.1.21+incompatible // indirect
47+
github.com/google/flatbuffers v25.2.10+incompatible // indirect
4748
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect
4849
github.com/google/uuid v1.6.0 // indirect
4950
github.com/huandu/xstrings v1.3.3 // indirect
@@ -57,7 +58,7 @@ require (
5758
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
5859
github.com/jackc/pgtype v1.14.0 // indirect
5960
github.com/jackc/pgx/v4 v4.18.3 // indirect
60-
github.com/klauspost/compress v1.17.8 // indirect
61+
github.com/klauspost/compress v1.18.0 // indirect
6162
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
6263
github.com/libdns/libdns v0.2.2 // indirect
6364
github.com/manifoldco/promptui v0.9.0 // indirect
@@ -89,14 +90,17 @@ require (
8990
github.com/smallstep/scep v0.0.0-20231024192529-aee96d7ad34d // indirect
9091
github.com/smallstep/truststore v0.13.0 // indirect
9192
github.com/spf13/cast v1.4.1 // indirect
92-
github.com/spf13/cobra v1.8.0 // indirect
93-
github.com/spf13/pflag v1.0.5 // indirect
93+
github.com/spf13/cobra v1.9.1 // indirect
94+
github.com/spf13/pflag v1.0.6 // indirect
9495
github.com/stoewer/go-strcase v1.2.0 // indirect
9596
github.com/tailscale/tscert v0.0.0-20240517230440-bbccfbf48933 // indirect
9697
github.com/urfave/cli v1.22.14 // indirect
9798
github.com/zeebo/blake3 v0.2.3 // indirect
9899
go.etcd.io/bbolt v1.3.9 // indirect
99-
go.opencensus.io v0.24.0 // indirect
100+
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
101+
go.opentelemetry.io/otel v1.37.0 // indirect
102+
go.opentelemetry.io/otel/metric v1.37.0 // indirect
103+
go.opentelemetry.io/otel/trace v1.37.0 // indirect
100104
go.step.sm/cli-utils v0.9.0 // indirect
101105
go.step.sm/crypto v0.45.0 // indirect
102106
go.step.sm/linkedca v0.20.1 // indirect
@@ -105,21 +109,21 @@ require (
105109
go.uber.org/multierr v1.11.0 // indirect
106110
go.uber.org/zap v1.27.0 // indirect
107111
go.uber.org/zap/exp v0.2.0 // indirect
108-
golang.org/x/crypto v0.23.0 // indirect
112+
golang.org/x/crypto v0.39.0 // indirect
109113
golang.org/x/crypto/x509roots/fallback v0.0.0-20240507223354-67b13616a595 // indirect
110114
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
111-
golang.org/x/mod v0.17.0 // indirect
112-
golang.org/x/net v0.25.0 // indirect
113-
golang.org/x/sync v0.7.0 // indirect
114-
golang.org/x/sys v0.20.0 // indirect
115-
golang.org/x/term v0.20.0 // indirect
116-
golang.org/x/text v0.15.0 // indirect
115+
golang.org/x/mod v0.25.0 // indirect
116+
golang.org/x/net v0.41.0 // indirect
117+
golang.org/x/sync v0.15.0 // indirect
118+
golang.org/x/sys v0.34.0 // indirect
119+
golang.org/x/term v0.32.0 // indirect
120+
golang.org/x/text v0.26.0 // indirect
117121
golang.org/x/time v0.5.0 // indirect
118-
golang.org/x/tools v0.21.0 // indirect
122+
golang.org/x/tools v0.33.0 // indirect
119123
google.golang.org/genproto/googleapis/api v0.0.0-20240506185236-b8a5c65736ae // indirect
120124
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 // indirect
121125
google.golang.org/grpc v1.64.0 // indirect
122-
google.golang.org/protobuf v1.36.5 // indirect
126+
google.golang.org/protobuf v1.36.6 // indirect
123127
gopkg.in/yaml.v3 v3.0.1 // indirect
124128
howett.net/plist v1.0.0 // indirect
125129
)

0 commit comments

Comments
 (0)