@@ -27,7 +27,7 @@ type MemoryCache[K comparable, V any] struct {
27
27
// New 创建缓存数据库实例
28
28
// Creating a Cached Database Instance
29
29
func New [K comparable , V any ](options ... Option ) * MemoryCache [K , V ] {
30
- var conf = & config {TimeCacheEnabled : true }
30
+ var conf = & config {CachedTime : true , LRU : true }
31
31
options = append (options , withInitialize ())
32
32
for _ , fn := range options {
33
33
fn (conf )
@@ -46,10 +46,10 @@ func New[K comparable, V any](options ...Option) *MemoryCache[K, V] {
46
46
47
47
for i , _ := range mc .storage {
48
48
mc .storage [i ] = & bucket [K , V ]{
49
- MaxCapacity : conf . MaxCapacity ,
50
- Map : containers .NewMap [K , * Element [K , V ]](conf .InitialSize , conf .SwissTable ),
51
- Heap : newHeap [K , V ](conf .InitialSize ),
52
- List : new ( queue [K , V ]),
49
+ conf : conf ,
50
+ Map : containers .NewMap [K , * Element [K , V ]](conf .BucketSize , conf .SwissTable ),
51
+ Heap : newHeap [K , V ](conf .BucketSize ),
52
+ List : newQueue [K , V ]( conf . LRU ),
53
53
}
54
54
}
55
55
@@ -66,11 +66,11 @@ func New[K comparable, V any](options ...Option) *MemoryCache[K, V] {
66
66
case now := <- ticker .C :
67
67
var sum = 0
68
68
for _ , b := range mc .storage {
69
- sum += b .ExpireCheck (now .UnixMilli (), conf .MaxKeysDeleted )
69
+ sum += b .Check (now .UnixMilli (), conf .DeleteLimits )
70
70
}
71
71
72
72
// 删除数量超过阈值, 缩小时间间隔
73
- if d1 := utils .SelectValue (sum > conf .BucketNum * conf .MaxKeysDeleted * 7 / 10 , conf .MinInterval , conf .MaxInterval ); d1 != d0 {
73
+ if d1 := utils .SelectValue (sum > conf .BucketNum * conf .DeleteLimits * 7 / 10 , conf .MinInterval , conf .MaxInterval ); d1 != d0 {
74
74
d0 = d1
75
75
ticker .Reset (d0 )
76
76
}
@@ -102,9 +102,9 @@ func New[K comparable, V any](options ...Option) *MemoryCache[K, V] {
102
102
func (c * MemoryCache [K , V ]) Clear () {
103
103
for _ , b := range c .storage {
104
104
b .Lock ()
105
- b .Heap = newHeap [K , V ](c .conf .InitialSize )
106
- b .Map = containers .NewMap [K , * Element [K , V ]](c .conf .InitialSize , c .conf .SwissTable )
107
- b .List = new ( queue [K , V ])
105
+ b .Heap = newHeap [K , V ](c .conf .BucketSize )
106
+ b .Map = containers .NewMap [K , * Element [K , V ]](c .conf .BucketSize , c .conf .SwissTable )
107
+ b .List = newQueue [K , V ]( c . conf . LRU )
108
108
b .Unlock ()
109
109
}
110
110
}
@@ -123,7 +123,7 @@ func (c *MemoryCache[K, V]) getBucket(key K) *bucket[K, V] {
123
123
}
124
124
125
125
func (c * MemoryCache [K , V ]) getTimestamp () int64 {
126
- if c .conf .TimeCacheEnabled {
126
+ if c .conf .CachedTime {
127
127
return c .timestamp .Load ()
128
128
}
129
129
return time .Now ().UnixMilli ()
@@ -168,15 +168,17 @@ func (c *MemoryCache[K, V]) SetWithCallback(key K, value V, exp time.Duration, c
168
168
var expireAt = c .getExp (exp )
169
169
ele , ok := c .fetch (b , key )
170
170
if ok {
171
- b .UpdateAll (ele , value , expireAt , cb )
171
+ ele .Value , ele .cb = value , cb
172
+ b .UpdateTTL (ele , expireAt )
172
173
return true
173
174
}
174
175
175
176
b .Insert (key , value , expireAt , cb )
176
177
return false
177
178
}
178
179
179
- // Get
180
+ // Get 查询缓存
181
+ // query cache
180
182
func (c * MemoryCache [K , V ]) Get (key K ) (v V , exist bool ) {
181
183
var b = c .getBucket (key )
182
184
b .Lock ()
@@ -229,7 +231,8 @@ func (c *MemoryCache[K, V]) GetOrCreateWithCallback(key K, value V, exp time.Dur
229
231
return value , false
230
232
}
231
233
232
- // Delete
234
+ // Delete 删除缓存
235
+ // delete cache
233
236
func (c * MemoryCache [K , V ]) Delete (key K ) (deleted bool ) {
234
237
var b = c .getBucket (key )
235
238
b .Lock ()
@@ -244,7 +247,8 @@ func (c *MemoryCache[K, V]) Delete(key K) (deleted bool) {
244
247
return true
245
248
}
246
249
247
- // Range
250
+ // Range 遍历缓存. 注意: 不要在回调函数里面操作 MemoryCache[K, V] 实例, 可能会造成死锁.
251
+ // Traverse the cache. Note: Do not manipulate MemoryCache[K, V] instances inside callback functions, as this may cause deadlocks.
248
252
func (c * MemoryCache [K , V ]) Range (f func (K , V ) bool ) {
249
253
var now = time .Now ().UnixMilli ()
250
254
for _ , b := range c .storage {
@@ -262,8 +266,8 @@ func (c *MemoryCache[K, V]) Range(f func(K, V) bool) {
262
266
}
263
267
}
264
268
265
- // Len 获取当前元素数量
266
- // Get the number of Elements
269
+ // Len 快速获取当前缓存元素数量, 不做过期检查.
270
+ // Quickly gets the current number of cached elements, without checking for expiration.
267
271
func (c * MemoryCache [K , V ]) Len () int {
268
272
var num = 0
269
273
for _ , b := range c .storage {
@@ -276,14 +280,14 @@ func (c *MemoryCache[K, V]) Len() int {
276
280
277
281
type bucket [K comparable , V any ] struct {
278
282
sync.Mutex
279
- MaxCapacity int
280
- Map containers.Map [K , * Element [K , V ]]
281
- Heap * heap [K , V ]
282
- List * queue [K , V ]
283
+ conf * config
284
+ Map containers.Map [K , * Element [K , V ]]
285
+ Heap * heap [K , V ]
286
+ List * queue [K , V ]
283
287
}
284
288
285
- // ExpireCheck 过期时间检查
286
- func (c * bucket [K , V ]) ExpireCheck (now int64 , num int ) int {
289
+ // Check 过期时间检查
290
+ func (c * bucket [K , V ]) Check (now int64 , num int ) int {
287
291
c .Lock ()
288
292
defer c .Unlock ()
289
293
@@ -302,20 +306,15 @@ func (c *bucket[K, V]) Delete(ele *Element[K, V], reason Reason) {
302
306
ele .cb (ele , reason )
303
307
}
304
308
305
- func (c * bucket [K , V ]) UpdateAll (ele * Element [K , V ], value V , expireAt int64 , cb CallbackFunc [* Element [K , V ]]) {
306
- ele .Value = value
307
- ele .cb = cb
308
- c .UpdateTTL (ele , expireAt )
309
- }
310
-
311
309
func (c * bucket [K , V ]) UpdateTTL (ele * Element [K , V ], expireAt int64 ) {
312
310
c .Heap .UpdateTTL (ele , expireAt )
313
311
c .List .MoveToBack (ele )
314
312
}
315
313
316
314
func (c * bucket [K , V ]) Insert (key K , value V , expireAt int64 , cb CallbackFunc [* Element [K , V ]]) {
317
- if c .List .Len () >= c .MaxCapacity {
318
- c .Delete (c .List .Front (), ReasonEvicted )
315
+ if c .Heap .Len () >= c .conf .BucketCap {
316
+ head := utils .SelectValue (c .conf .LRU , c .List .Front (), c .Heap .Front ())
317
+ c .Delete (head , ReasonEvicted )
319
318
}
320
319
321
320
var ele = & Element [K , V ]{Key : key , Value : value , ExpireAt : expireAt , cb : cb }
0 commit comments