-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgog.go
53 lines (42 loc) · 910 Bytes
/
gog.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
package cache_bench
import (
"context"
"time"
cache "github.com/Code-Hex/go-generics-cache"
"github.com/c-pro/geche"
)
type gogLRU[K comparable, V any] struct {
c *cache.Cache[K, V]
ttl time.Duration
}
func NewGogLRU[K comparable, V any](
ctx context.Context,
ttl, cleanInterval time.Duration,
) *gogLRU[K, V] {
c := cache.NewContext(ctx, cache.WithJanitorInterval[K, V](cleanInterval))
return &gogLRU[K, V]{
c: c,
ttl: ttl,
}
}
func (g *gogLRU[K, V]) Set(key K, value V) {
g.c.Set(key, value, cache.WithExpiration(g.ttl))
}
func (g *gogLRU[K, V]) Get(key K) (V, error) {
val, ok := g.c.Get(key)
if !ok {
return val, geche.ErrNotFound
}
return val, nil
}
func (g *gogLRU[K, V]) Del(key K) error {
g.c.Delete(key)
return nil
}
func (g *gogLRU[K, V]) Len() int {
return len(g.c.Keys())
}
func (g *gogLRU[K, V]) Snapshot() map[K]V {
// not used in benchmark
return nil
}