-
Notifications
You must be signed in to change notification settings - Fork 8
/
ttl_cache.go
162 lines (144 loc) · 5.31 KB
/
ttl_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2023-2024 Phus Lu. All rights reserved.
package lru
import (
"context"
"sync/atomic"
"time"
"unsafe"
)
// TTLCache implements LRU Cache with TTL functionality.
type TTLCache[K comparable, V any] struct {
shards [512]ttlshard[K, V]
mask uint32
hasher func(key unsafe.Pointer, seed uintptr) uintptr
seed uintptr
loader func(ctx context.Context, key K) (value V, ttl time.Duration, err error)
group singleflightGroup[K, V]
}
// NewTTLCache creates lru cache with size capacity.
func NewTTLCache[K comparable, V any](size int, options ...Option[K, V]) *TTLCache[K, V] {
clocking()
j := -1
for i, o := range options {
if _, ok := o.(*shardsOption[K, V]); ok {
j = i
}
}
switch {
case j < 0:
options = append([]Option[K, V]{WithShards[K, V](0)}, options...)
case j > 0:
options[0], options[j] = options[j], options[0]
}
c := new(TTLCache[K, V])
for _, o := range options {
o.applyToTTLCache(c)
}
if c.hasher == nil {
c.hasher = getRuntimeHasher[K]()
}
if c.seed == 0 {
c.seed = uintptr(fastrand64())
}
if isamd64 {
// pre-alloc lists and tables for compactness
shardsize := (uint32(size) + c.mask) / (c.mask + 1)
shardlists := make([]ttlnode[K, V], (shardsize+1)*(c.mask+1))
tablesize := ttlNewTableSize(uint32(shardsize))
tablebuckets := make([]uint64, tablesize*(c.mask+1))
for i := uint32(0); i <= c.mask; i++ {
c.shards[i].list = shardlists[i*(shardsize+1) : (i+1)*(shardsize+1)]
c.shards[i].tableBuckets = tablebuckets[i*tablesize : (i+1)*tablesize]
c.shards[i].Init(shardsize, c.hasher, c.seed)
}
} else {
shardsize := (uint32(size) + c.mask) / (c.mask + 1)
for i := uint32(0); i <= c.mask; i++ {
c.shards[i].Init(shardsize, c.hasher, c.seed)
}
}
return c
}
// Get returns value for key.
func (c *TTLCache[K, V]) Get(key K) (value V, ok bool) {
hash := uint32(c.hasher(noescape(unsafe.Pointer(&key)), c.seed))
// return c.shards[hash&c.mask].Get(hash, key)
return (*ttlshard[K, V])(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).Get(hash, key)
}
// GetOrLoad returns value for key, call loader function by singleflight if value was not in cache.
func (c *TTLCache[K, V]) GetOrLoad(ctx context.Context, key K, loader func(context.Context, K) (V, time.Duration, error)) (value V, err error, ok bool) {
hash := uint32(c.hasher(noescape(unsafe.Pointer(&key)), c.seed))
// value, ok = c.shards[hash&c.mask].Get(hash, key)
value, ok = (*ttlshard[K, V])(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).Get(hash, key)
if !ok {
if loader == nil {
loader = c.loader
}
if loader == nil {
err = ErrLoaderIsNil
return
}
value, err, ok = c.group.Do(key, func() (V, error) {
v, ttl, err := loader(ctx, key)
if err != nil {
return v, err
}
c.shards[hash&c.mask].Set(hash, key, v, ttl)
return v, nil
})
}
return
}
// Peek returns value and expires nanoseconds for key, but does not modify its recency.
func (c *TTLCache[K, V]) Peek(key K) (value V, expires int64, ok bool) {
hash := uint32(c.hasher(noescape(unsafe.Pointer(&key)), c.seed))
// return c.shards[hash&c.mask].Peek(hash, key)
return (*ttlshard[K, V])(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).Peek(hash, key)
}
// Set inserts key value pair and returns previous value.
func (c *TTLCache[K, V]) Set(key K, value V, ttl time.Duration) (prev V, replaced bool) {
hash := uint32(c.hasher(noescape(unsafe.Pointer(&key)), c.seed))
// return c.shards[hash&c.mask].Set(hash, key, value, ttl)
return (*ttlshard[K, V])(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).Set(hash, key, value, ttl)
}
// SetIfAbsent inserts key value pair and returns previous value, if key is absent in the cache.
func (c *TTLCache[K, V]) SetIfAbsent(key K, value V, ttl time.Duration) (prev V, replaced bool) {
hash := uint32(c.hasher(noescape(unsafe.Pointer(&key)), c.seed))
// return c.shards[hash&c.mask].SetIfAbsent(hash, key, value, ttl)
return (*ttlshard[K, V])(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).SetIfAbsent(hash, key, value, ttl)
}
// Delete method deletes value associated with key and returns deleted value (or empty value if key was not in cache).
func (c *TTLCache[K, V]) Delete(key K) (prev V) {
hash := uint32(c.hasher(noescape(unsafe.Pointer(&key)), c.seed))
// return c.shards[hash&c.mask].Delete(hash, key)
return (*ttlshard[K, V])(unsafe.Add(unsafe.Pointer(&c.shards[0]), uintptr(hash&c.mask)*unsafe.Sizeof(c.shards[0]))).Delete(hash, key)
}
// Len returns number of cached nodes.
func (c *TTLCache[K, V]) Len() int {
var n uint32
for i := uint32(0); i <= c.mask; i++ {
n += c.shards[i].Len()
}
return int(n)
}
// AppendKeys appends all keys to keys and return the keys.
func (c *TTLCache[K, V]) AppendKeys(keys []K) []K {
now := atomic.LoadUint32(&clock)
for i := uint32(0); i <= c.mask; i++ {
keys = c.shards[i].AppendKeys(keys, now)
}
return keys
}
// Stats returns cache stats.
func (c *TTLCache[K, V]) Stats() (stats Stats) {
for i := uint32(0); i <= c.mask; i++ {
s := &c.shards[i]
s.mu.Lock()
stats.EntriesCount += uint64(s.tableLength)
stats.GetCalls += s.statsGetCalls
stats.SetCalls += s.statsSetCalls
stats.Misses += s.statsMisses
s.mu.Unlock()
}
return
}