-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeine.go
56 lines (44 loc) · 891 Bytes
/
theine.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
package cache_bench
import (
"time"
"github.com/Yiling-J/theine-go"
"github.com/c-pro/geche"
)
type theineCache[K comparable, V any] struct {
c *theine.Cache[K, V]
ttl time.Duration
}
func NewTheine[K comparable, V any](
size int64,
ttl time.Duration,
) *theineCache[K, V] {
c, err := theine.NewBuilder[K, V](size).Build()
if err != nil {
panic(err)
}
return &theineCache[K, V]{
c: c,
ttl: ttl,
}
}
func (t *theineCache[K, V]) Set(key K, value V) {
t.c.SetWithTTL(key, value, 1, t.ttl)
}
func (t *theineCache[K, V]) Get(key K) (V, error) {
val, ok := t.c.Get(key)
if !ok {
return val, geche.ErrNotFound
}
return val, nil
}
func (t *theineCache[K, V]) Del(key K) error {
t.c.Delete(key)
return nil
}
func (t *theineCache[K, V]) Len() int {
return t.c.Len()
}
func (t *theineCache[K, V]) Snapshot() map[K]V {
// not used in benchmark
return nil
}