-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbadger_cache_engine.go
146 lines (121 loc) · 3.05 KB
/
badger_cache_engine.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
package c_polygonid
import (
"encoding/json"
"errors"
"log/slog"
"time"
"github.com/dgraph-io/badger/v4"
"github.com/iden3/go-schema-processor/v2/loaders"
"github.com/piprate/json-gold/ld"
)
type cachedRemoteDocument struct {
RemoteDocument *ld.RemoteDocument
ExpireTime time.Time
}
type badgerCacheEngine struct {
embedDocs map[string]*ld.RemoteDocument
cacheDir string
}
func (m *badgerCacheEngine) Get(
key string) (*ld.RemoteDocument, time.Time, error) {
if m.embedDocs != nil {
doc, ok := m.embedDocs[key]
if ok {
return doc, time.Now().Add(time.Hour), nil
}
}
db, cleanup, err := getCacheDB(m.cacheDir)
if err != nil {
slog.Error("can't get cache database", "err", err)
return nil, time.Time{}, loaders.ErrCacheMiss
}
defer cleanup()
var value []byte
err = db.View(func(txn *badger.Txn) error {
entry, err := txn.Get([]byte(key))
if err != nil {
return err
}
value, err = entry.ValueCopy(nil)
return err
})
if errors.Is(err, badger.ErrKeyNotFound) {
return nil, time.Time{}, loaders.ErrCacheMiss
} else if err != nil {
slog.Error("error getting remote document from cache",
"err", err)
return nil, time.Time{}, loaders.ErrCacheMiss
}
var doc cachedRemoteDocument
err = json.Unmarshal(value, &doc)
if err != nil {
slog.Error("error unmarshalling cached document",
"err", err)
return nil, time.Time{}, loaders.ErrCacheMiss
}
return doc.RemoteDocument, doc.ExpireTime, nil
}
func (m *badgerCacheEngine) Set(key string, doc *ld.RemoteDocument,
expireTime time.Time) error {
if m.embedDocs != nil {
// if we have the document in the embedded cache, do not overwrite it
// with the new value.
_, ok := m.embedDocs[key]
if ok {
return nil
}
}
db, cleanup, err := getCacheDB(m.cacheDir)
if err != nil {
slog.Error("can't get cache database", "err", err)
return nil
}
defer cleanup()
value, err := json.Marshal(cachedRemoteDocument{
RemoteDocument: doc,
ExpireTime: expireTime,
})
if err != nil {
slog.Error("error marshalling cached document", "err", err)
return nil
}
err = db.Update(func(txn *badger.Txn) error {
return txn.Set([]byte(key), value)
})
if err != nil {
slog.Error("error storing document to BadgerDB", "err", err)
}
return nil
}
type badgerCacheEngineOption func(*badgerCacheEngine) error
func withEmbeddedDocumentBytes(u string, doc []byte) badgerCacheEngineOption {
return func(engine *badgerCacheEngine) error {
if engine.embedDocs == nil {
engine.embedDocs = make(map[string]*ld.RemoteDocument)
}
var rd = &ld.RemoteDocument{DocumentURL: u}
err := json.Unmarshal(doc, &rd.Document)
if err != nil {
return err
}
engine.embedDocs[u] = rd
return nil
}
}
func withCacheDir(cacheDir string) badgerCacheEngineOption {
return func(engine *badgerCacheEngine) error {
engine.cacheDir = cacheDir
return nil
}
}
func newBadgerCacheEngine(
opts ...badgerCacheEngineOption) (loaders.CacheEngine, error) {
e := &badgerCacheEngine{}
for _, opt := range opts {
err := opt(e)
if err != nil {
return nil, err
}
}
return e, nil
}