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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
package cache
import (
"errors"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/rogpeppe/go-internal/lockedfile"
"github.com/vmihailenco/msgpack/v5"
)
// The Core interface for cache
type Cacher[K any, V any] interface {
Clear()
Close() error
Cost() uint64
Delete(key K) error
Error() error
Flush() error
Get(key K, value *V) (time.Duration, error)
GetValue(key K) (V, time.Duration, error)
Set(key K, value V, ttl time.Duration) error
SetConfig(options ...Option) error
Memorize(key K, factoryFunc func() (V, error), ttl time.Duration) (V, error)
UpdateInPlace(key K, processFunc func(V) (V, error), ttl time.Duration) error
}
// cache represents a cache database with file-backed storage and in-memory operation.
type cache struct {
File io.WriteSeeker
Store store
Stop chan struct{}
wg sync.WaitGroup
err error
}
// Option is a function type for configuring the cache.
type Option func(*cache) error
// open opens a file-backed cache database with the given options.
func open(filename string, options ...Option) (*cache, error) {
ret := &cache{}
ret.Store.Init()
if err := ret.SetConfig(options...); err != nil {
return nil, err
}
if filename == "" {
return ret, nil
}
file, err := lockedfile.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0o666)
if err != nil {
return nil, err
}
fileInfo, err := file.Stat()
if err != nil {
return nil, err
}
if fileInfo.Size() == 0 {
ret.File = file
if err := ret.Flush(); err != nil {
return nil, err
}
} else {
err := ret.Store.LoadSnapshot(file)
if err != nil {
return nil, err
}
ret.File = file
}
return ret, nil
}
// start begins the background worker for periodic tasks.
func (c *cache) start() {
c.Stop = make(chan struct{})
c.wg.Add(1)
go c.backgroundWorker()
}
// SetConfig applies configuration options to the cache.
func (c *cache) SetConfig(options ...Option) error {
c.Store.Lock.Lock()
defer c.Store.Lock.Unlock()
for _, opt := range options {
if err := opt(c); err != nil {
return err
}
}
return nil
}
// WithPolicy sets the eviction policy for the cache.
func WithPolicy(e EvictionPolicyType) Option {
return func(d *cache) error {
return d.Store.Policy.SetPolicy(e)
}
}
// WithMaxCost sets the maximum cost for the cache.
func WithMaxCost(maxCost uint64) Option {
return func(d *cache) error {
d.Store.MaxCost = maxCost
return nil
}
}
// SetSnapshotTime sets the interval for taking snapshots of the cache.
func SetSnapshotTime(t time.Duration) Option {
return func(d *cache) error {
d.Store.SnapshotTicker.Reset(t)
return nil
}
}
// SetCleanupTime sets the interval for cleaning up expired entries.
func SetCleanupTime(t time.Duration) Option {
return func(d *cache) error {
d.Store.CleanupTicker.Reset(t)
return nil
}
}
// backgroundWorker performs periodic tasks such as snapshotting and cleanup.
func (c *cache) backgroundWorker() {
defer c.wg.Done()
defer func() {
if r := recover(); r != nil {
c.err = fmt.Errorf("panic occurred: %v", r)
}
}()
c.Store.SnapshotTicker.Resume()
defer c.Store.SnapshotTicker.Stop()
c.Store.CleanupTicker.Resume()
defer c.Store.CleanupTicker.Stop()
c.Store.Cleanup()
c.Store.Evict()
for {
select {
case <-c.Stop:
return
case <-c.Store.SnapshotTicker.C:
if err := c.Flush(); err != nil {
c.err = err
}
case <-c.Store.CleanupTicker.C:
c.Store.Cleanup()
c.Store.Evict()
}
}
}
func (c *cache) Error() error {
return c.err
}
func (c *cache) Cost() uint64 {
return c.Store.Cost
}
// Close stops the background worker and cleans up resources.
func (c *cache) Close() error {
close(c.Stop)
c.wg.Wait()
err := c.Flush()
c.Clear()
var err1 error
if c.File != nil {
closer, ok := c.File.(io.Closer)
if ok {
err1 = closer.Close()
}
}
if err != nil {
return err
}
return err1
}
// Flush writes the current state of the store to the file.
func (c *cache) Flush() error {
if c.File != nil {
return c.Store.Snapshot(c.File)
}
return nil
}
// Clear removes all entries from the in-memory store.
func (c *cache) Clear() {
c.Store.Clear()
}
var ErrKeyNotFound = errors.New("key not found") // ErrKeyNotFound is returned when a key is not found in the cache.
// Get retrieves a value from the cache by key and returns its TTL.
func (c *cache) Get(key []byte, value *[]byte) (time.Duration, error) {
v, ttl, err := c.GetValue(key)
*value = v
return ttl, err
}
// GetValue retrieves a value from the cache by key and returns the value and its TTL.
func (c *cache) GetValue(key []byte) ([]byte, time.Duration, error) {
if err := c.err; err != nil {
return zero[[]byte](), 0, err
}
v, ttl, ok := c.Store.Get(key)
if !ok {
return v, 0, ErrKeyNotFound
}
return v, ttl, nil
}
// Set adds a key-value pair to the cache with a specified TTL.
func (c *cache) Set(key, value []byte, ttl time.Duration) error {
if err := c.err; err != nil {
return err
}
c.Store.Set(key, value, ttl)
return nil
}
// Delete removes a key-value pair from the cache.
func (c *cache) Delete(key []byte) error {
ok := c.Store.Delete(key)
if !ok {
return ErrKeyNotFound
}
return nil
}
// UpdateInPlace retrieves a value from the cache, processes it using the provided function,
// and then sets the result back into the cache with the same key.
func (c *cache) UpdateInPlace(key []byte, processFunc func([]byte) ([]byte, error), ttl time.Duration) error {
if err := c.err; err != nil {
return err
}
return c.Store.UpdateInPlace(key, processFunc, ttl)
}
// Memorize attempts to retrieve a value from the cache. If the retrieval fails,
// it sets the result of the factory function into the cache and returns that result.
func (c *cache) Memorize(key []byte, factoryFunc func() ([]byte, error), ttl time.Duration) ([]byte, error) {
if err := c.err; err != nil {
return []byte{}, err
}
return c.Store.Memorize(key, factoryFunc, ttl)
}
// The Cache database. Can be initialized by either Open or OpenFile or OpenMem. Uses per Cache Locks.
// Cache represents a generic cache database with key-value pairs.
type Cache[K any, V any] struct {
*cache
}
var _ Cacher[any, any] = Cache[any, any]{}
// The CacheRaw database. Can be initialized by either OpenRaw or OpenRawFile or OpenRawMem. Uses per Cache Locks.
// CacheRaw represents a binary cache database with key-value pairs.
type CacheRaw struct {
*cache
}
var _ Cacher[[]byte, []byte] = CacheRaw{}
// OpenRaw opens a binary cache database with the specified options. If filename is empty then in-memory otherwise file backed.
func OpenRaw(filename string, options ...Option) (CacheRaw, error) {
ret, err := open(filename, options...)
if err != nil {
return zero[CacheRaw](), err
}
ret.start()
return CacheRaw{cache: ret}, nil
}
var ErrEmptyFilename = errors.New("cannot open empty filename")
// OpenRawFile opens a binary file-backed cache database with the specified options.
func OpenRawFile(filename string, options ...Option) (CacheRaw, error) {
if filename == "" {
return zero[CacheRaw](), ErrEmptyFilename
}
return OpenRaw(filename, options...)
}
// OpenRawMem initializes a binary in-memory cache database with the specified options.
func OpenRawMem(options ...Option) (CacheRaw, error) {
return OpenRaw("", options...)
}
// Open opens a cache database with the specified options. If filename is empty then in-memory otherwise file backed.
func Open[K, V any](filename string, options ...Option) (Cache[K, V], error) {
ret, err := OpenRaw(filename, options...)
if err != nil {
return zero[Cache[K, V]](), err
}
return Cache[K, V]{cache: ret.cache}, nil
}
// OpenFile opens a file-backed cache database with the specified options.
func OpenFile[K, V any](filename string, options ...Option) (Cache[K, V], error) {
if filename == "" {
return zero[Cache[K, V]](), ErrEmptyFilename
}
return Open[K, V](filename, options...)
}
// OpenMem initializes an in-memory cache database with the specified options.
func OpenMem[K, V any](options ...Option) (Cache[K, V], error) {
return Open[K, V]("", options...)
}
// marshal serializes a value using msgpack.
func marshal[T any](v T) ([]byte, error) {
return msgpack.Marshal(v)
}
// unmarshal deserializes data into a value using msgpack.
func unmarshal[T any](data []byte, v *T) error {
return msgpack.Unmarshal(data, v)
}
// Get retrieves a value from the cache by key and returns its TTL.
func (c Cache[K, V]) Get(key K, value *V) (time.Duration, error) {
keyData, err := marshal(key)
if err != nil {
return 0, err
}
v, ttl, err := c.cache.GetValue(keyData)
if err != nil {
return 0, err
}
if v != nil {
if err = unmarshal(v, value); err != nil {
return 0, err
}
}
return ttl, err
}
// GetValue retrieves a value from the cache by key and returns the value and its TTL.
func (c Cache[K, V]) GetValue(key K) (V, time.Duration, error) {
value := zero[V]()
ttl, err := c.Get(key, &value)
return value, ttl, err
}
// Set adds a key-value pair to the cache with a specified TTL.
func (c Cache[K, V]) Set(key K, value V, ttl time.Duration) error {
keyData, err := marshal(key)
if err != nil {
return err
}
valueData, err := marshal(value)
if err != nil {
return err
}
return c.cache.Set(keyData, valueData, ttl)
}
// Delete removes a key-value pair from the cache.
func (c Cache[K, V]) Delete(key K) error {
keyData, err := marshal(key)
if err != nil {
return err
}
return c.cache.Delete(keyData)
}
// UpdateInPlace retrieves a value from the cache, processes it using the provided function,
// and then sets the result back into the cache with the same key.
func (c Cache[K, V]) UpdateInPlace(key K, processFunc func(V) (V, error), ttl time.Duration) error {
keyData, err := marshal(key)
if err != nil {
return err
}
return c.cache.UpdateInPlace(keyData, func(data []byte) ([]byte, error) {
var value V
if err := unmarshal(data, &value); err != nil {
return nil, err
}
processedValue, err := processFunc(value)
if err != nil {
return nil, err
}
return marshal(processedValue)
}, ttl)
}
// Memorize attempts to retrieve a value from the cache. If the retrieval fails,
// it sets the result of the factory function into the cache and returns that result.
func (c Cache[K, V]) Memorize(key K, factoryFunc func() (V, error), ttl time.Duration) (V, error) {
keyData, err := marshal(key)
if err != nil {
return zero[V](), err
}
data, err := c.cache.Memorize(keyData, func() ([]byte, error) {
value, err := factoryFunc()
if err != nil {
return nil, err
}
return marshal(value)
}, ttl)
if err != nil {
return zero[V](), err
}
var value V
if err := unmarshal(data, &value); err != nil {
return zero[V](), err
}
return value, nil
}
|