@@ -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 {CachedTime : 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 )
@@ -66,7 +66,7 @@ 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 .DeleteLimits )
69
+ sum += b .Check (now .UnixMilli (), conf .DeleteLimits )
70
70
}
71
71
72
72
// 删除数量超过阈值, 缩小时间间隔
@@ -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 {
@@ -282,8 +286,8 @@ type bucket[K comparable, V any] struct {
282
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,12 +306,6 @@ 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 )
0 commit comments