forked from fiatjaf/njump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
139 lines (119 loc) · 2.61 KB
/
cache.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
//go:build !nocache
package main
import (
"encoding/json"
"strings"
"time"
"github.com/dgraph-io/badger/v4"
)
var cache = Cache{}
type Cache struct {
*badger.DB
}
func (c *Cache) initializeCache() func() {
db, err := badger.Open(badger.DefaultOptions(s.DiskCachePath))
if err != nil {
log.Fatal().Err(err).Str("path", s.DiskCachePath).Msg("failed to open badger")
}
c.DB = db
go func() {
ticker := time.NewTicker(2 * time.Hour)
defer ticker.Stop()
for range ticker.C {
again:
err := db.RunValueLogGC(0.8)
if err == nil {
goto again
}
}
}()
return func() {
db.Close()
}
}
func (c *Cache) Delete(key string) error {
return c.DB.Update(func(txn *badger.Txn) error {
return txn.Delete([]byte(key))
})
}
func (c *Cache) Get(key string) ([]byte, bool) {
var val []byte
err := c.DB.View(func(txn *badger.Txn) error {
b, err := txn.Get([]byte(key))
if err != nil {
return err
}
val, err = b.ValueCopy(nil)
return err
})
if err == badger.ErrKeyNotFound {
return nil, false
}
if err != nil {
log.Fatal().Err(err).Str("key", key).Msg("error getting key from cache")
}
return val, true
}
func (c *Cache) GetPaginatedKeys(prefix string, page int, size int) []string {
keys := []string{}
err := c.DB.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
start := (page-1)*size + 1
index := 1
for it.Seek([]byte(prefix)); it.ValidForPrefix([]byte(prefix)); it.Next() {
if index < start {
index++
continue
}
if index > start+size-1 {
break
}
item := it.Item()
k := item.Key()
keys = append(keys, strings.TrimPrefix(string(k), prefix+":"))
index++
}
return nil
})
if err != nil {
log.Fatal().Err(err).Msg("")
}
return keys
}
func (c *Cache) GetJSON(key string, recv any) bool {
b, ok := c.Get(key)
if !ok {
return ok
}
json.Unmarshal(b, recv)
return true
}
func (c *Cache) Set(key string, value []byte) {
err := c.DB.Update(func(txn *badger.Txn) error {
return txn.Set([]byte(key), value)
})
if err != nil {
log.Fatal().Err(err).Msg("")
}
}
func (c *Cache) SetJSON(key string, value any) {
j, _ := json.Marshal(value)
c.Set(key, j)
}
func (c *Cache) SetWithTTL(key string, value []byte, ttl time.Duration) {
err := c.DB.Update(func(txn *badger.Txn) error {
return txn.SetEntry(
badger.NewEntry([]byte(key), value).WithTTL(ttl),
)
})
if err != nil {
log.Fatal().Err(err).Msg("")
}
}
func (c *Cache) SetJSONWithTTL(key string, value any, ttl time.Duration) {
j, _ := json.Marshal(value)
c.SetWithTTL(key, j, ttl)
}