Repository navigation
Expand file tree
/
Copy pathfifo.go
More file actions
204 lines (187 loc) · 3.88 KB
/
Copy pathfifo.go
File metadata and controls
204 lines (187 loc) · 3.88 KB
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
// Copyright 2024 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fifocache
import (
"sync"
"sync/atomic"
)
// Cache implements an in-memory cache with FIFO-based eviction
// it's mostly like the S3-fifo, only without the ghost queue part
type Cache[K comparable, V any] struct {
capacity int
capacity1 int
onEvict func(K, V)
keyShardFunc func(K) uint8
shards [256]struct {
sync.RWMutex
values map[K]*_CacheItem[K, V]
}
queueLock sync.Mutex
used1 int
queue1 Queue[*_CacheItem[K, V]]
used2 int
queue2 Queue[*_CacheItem[K, V]]
}
type _CacheItem[K comparable, V any] struct {
key K
value V
size int
count atomic.Int32
}
func (c *_CacheItem[K, V]) inc() {
for {
cur := c.count.Load()
if cur >= 3 {
return
}
if c.count.CompareAndSwap(cur, cur+1) {
return
}
}
}
func (c *_CacheItem[K, V]) dec() {
for {
cur := c.count.Load()
if cur <= 0 {
return
}
if c.count.CompareAndSwap(cur, cur-1) {
return
}
}
}
func New[K comparable, V any](
capacity int,
onEvict func(K, V),
keyShardFunc func(K) uint8,
) *Cache[K, V] {
ret := &Cache[K, V]{
capacity: capacity,
capacity1: capacity / 10,
queue1: *NewQueue[*_CacheItem[K, V]](),
queue2: *NewQueue[*_CacheItem[K, V]](),
onEvict: onEvict,
keyShardFunc: keyShardFunc,
}
for i := range ret.shards {
ret.shards[i].values = make(map[K]*_CacheItem[K, V], 1024)
}
return ret
}
func (c *Cache[K, V]) Set(key K, value V, size int) {
shard := &c.shards[c.keyShardFunc(key)]
shard.Lock()
_, ok := shard.values[key]
if ok {
// existed
shard.Unlock()
return
}
item := &_CacheItem[K, V]{
key: key,
value: value,
size: size,
}
shard.values[key] = item
shard.Unlock()
c.queueLock.Lock()
defer c.queueLock.Unlock()
c.queue1.enqueue(item)
c.used1 += size
if c.used1+c.used2 > c.capacity {
c.evict()
}
}
func (c *Cache[K, V]) Get(key K) (value V, ok bool) {
shard := &c.shards[c.keyShardFunc(key)]
shard.RLock()
var item *_CacheItem[K, V]
item, ok = shard.values[key]
if !ok {
shard.RUnlock()
return
}
shard.RUnlock()
item.inc()
return item.value, true
}
func (c *Cache[K, V]) Delete(key K) {
shard := &c.shards[c.keyShardFunc(key)]
shard.Lock()
defer shard.Unlock()
delete(shard.values, key)
// we don't update queues
}
func (c *Cache[K, V]) evict() {
for c.used1+c.used2 > c.capacity {
if c.used1 > c.capacity1 {
c.evict1()
} else {
c.evict2()
}
}
}
func (c *Cache[K, V]) evict1() {
// queue 1
for {
item, ok := c.queue1.dequeue()
if !ok {
// queue empty
return
}
if item.count.Load() > 1 {
// put queue2
c.queue2.enqueue(item)
c.used1 -= item.size
c.used2 += item.size
} else {
// evict
shard := &c.shards[c.keyShardFunc(item.key)]
shard.Lock()
delete(shard.values, item.key)
shard.Unlock()
if c.onEvict != nil {
c.onEvict(item.key, item.value)
}
c.used1 -= item.size
return
}
}
}
func (c *Cache[K, V]) evict2() {
// queue 2
for {
item, ok := c.queue2.dequeue()
if !ok {
// empty queue
break
}
if item.count.Load() > 0 {
// re-enqueue
c.queue2.enqueue(item)
item.dec()
} else {
// evict
shard := &c.shards[c.keyShardFunc(item.key)]
shard.Lock()
delete(shard.values, item.key)
shard.Unlock()
if c.onEvict != nil {
c.onEvict(item.key, item.value)
}
c.used2 -= item.size
return
}
}
}