-
Notifications
You must be signed in to change notification settings - Fork 0
/
badger.go
185 lines (154 loc) · 3.85 KB
/
badger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package remember
import (
"github.com/dgraph-io/badger/v3"
"time"
)
// BadgerCache is the type for a Badger database cache.
type BadgerCache struct {
Conn *badger.DB
Prefix string
}
func (b *BadgerCache) Has(str string) bool {
_, err := b.Get(str)
if err != nil {
return false
}
return true
}
// Close closes the badger database.
func (b *BadgerCache) Close() error {
return b.Conn.Close()
}
// Get attempts to retrieve a value from the cache.
func (b *BadgerCache) Get(str string) (any, error) {
var fromCache []byte
err := b.Conn.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(str))
if err != nil {
return err
}
err = item.Value(func(val []byte) error {
fromCache = append([]byte{}, val...)
return nil
})
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
decoded, err := decode(string(fromCache))
if err != nil {
return nil, err
}
item := decoded[str]
return item, nil
}
// Set puts a value into Badger. The final parameter, expires, is optional.
func (b *BadgerCache) Set(str string, value any, expires ...time.Duration) error {
entry := CacheEntry{}
entry[str] = value
encoded, err := encode(entry)
if err != nil {
return err
}
if len(expires) > 0 {
err = b.Conn.Update(func(txn *badger.Txn) error {
e := badger.NewEntry([]byte(str), encoded).WithTTL(expires[0])
err = txn.SetEntry(e)
return err
})
} else {
err = b.Conn.Update(func(txn *badger.Txn) error {
e := badger.NewEntry([]byte(str), encoded)
err = txn.SetEntry(e)
return err
})
}
return nil
}
// Forget removes an item from the cache, by key.
func (b *BadgerCache) Forget(str string) error {
err := b.Conn.Update(func(txn *badger.Txn) error {
err := txn.Delete([]byte(str))
return err
})
return err
}
// EmptyByMatch removes all entries in Redis which have the prefix match.
func (b *BadgerCache) EmptyByMatch(str string) error {
return b.emptyByMatch(str)
}
// Empty removes all entries in Badger.
func (b *BadgerCache) Empty() error {
return b.emptyByMatch("")
}
func (b *BadgerCache) emptyByMatch(str string) error {
deleteKeys := func(keysForDelete [][]byte) error {
if err := b.Conn.Update(func(txn *badger.Txn) error {
for _, key := range keysForDelete {
if err := txn.Delete(key); err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
return nil
}
collectSize := 100000
err := b.Conn.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.AllVersions = false
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
keysForDelete := make([][]byte, 0, collectSize)
keysCollected := 0
for it.Seek([]byte(str)); it.ValidForPrefix([]byte(str)); it.Next() {
key := it.Item().KeyCopy(nil)
keysForDelete = append(keysForDelete, key)
keysCollected++
if keysCollected == collectSize {
if err := deleteKeys(keysForDelete); err != nil {
return err
}
}
}
if keysCollected > 0 {
if err := deleteKeys(keysForDelete); err != nil {
return err
}
}
return nil
})
return err
}
// GetInt is a convenience method which retrieves a value from the cache, converts it to an int, and returns it.
func (b *BadgerCache) GetInt(key string) (int, error) {
val, err := b.Get(key)
if err != nil {
return 0, err
}
return val.(int), nil
}
// GetString is a convenience method which retrieves a value from the cache and returns it as a string.
func (b *BadgerCache) GetString(key string) (string, error) {
s, err := b.Get(key)
if err != nil {
return "", err
}
return s.(string), nil
}
// GetTime retrieves a value from the cache by the specified key and returns it as time.Time.
func (b *BadgerCache) GetTime(key string) (time.Time, error) {
fromCache, err := b.Get(key)
if err != nil {
return time.Time{}, err
}
t := fromCache.(time.Time)
return t, nil
}