Skip to content

Commit ad3b402

Browse files
committed
enable lru by default
1 parent 7ce6f7d commit ad3b402

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

cache.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type MemoryCache[K comparable, V any] struct {
2727
// New 创建缓存数据库实例
2828
// Creating a Cached Database Instance
2929
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}
3131
options = append(options, withInitialize())
3232
for _, fn := range options {
3333
fn(conf)
@@ -66,7 +66,7 @@ func New[K comparable, V any](options ...Option) *MemoryCache[K, V] {
6666
case now := <-ticker.C:
6767
var sum = 0
6868
for _, b := range mc.storage {
69-
sum += b.ExpireCheck(now.UnixMilli(), conf.DeleteLimits)
69+
sum += b.Check(now.UnixMilli(), conf.DeleteLimits)
7070
}
7171

7272
// 删除数量超过阈值, 缩小时间间隔
@@ -168,15 +168,17 @@ func (c *MemoryCache[K, V]) SetWithCallback(key K, value V, exp time.Duration, c
168168
var expireAt = c.getExp(exp)
169169
ele, ok := c.fetch(b, key)
170170
if ok {
171-
b.UpdateAll(ele, value, expireAt, cb)
171+
ele.Value, ele.cb = value, cb
172+
b.UpdateTTL(ele, expireAt)
172173
return true
173174
}
174175

175176
b.Insert(key, value, expireAt, cb)
176177
return false
177178
}
178179

179-
// Get
180+
// Get 查询缓存
181+
// query cache
180182
func (c *MemoryCache[K, V]) Get(key K) (v V, exist bool) {
181183
var b = c.getBucket(key)
182184
b.Lock()
@@ -229,7 +231,8 @@ func (c *MemoryCache[K, V]) GetOrCreateWithCallback(key K, value V, exp time.Dur
229231
return value, false
230232
}
231233

232-
// Delete
234+
// Delete 删除缓存
235+
// delete cache
233236
func (c *MemoryCache[K, V]) Delete(key K) (deleted bool) {
234237
var b = c.getBucket(key)
235238
b.Lock()
@@ -244,7 +247,8 @@ func (c *MemoryCache[K, V]) Delete(key K) (deleted bool) {
244247
return true
245248
}
246249

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.
248252
func (c *MemoryCache[K, V]) Range(f func(K, V) bool) {
249253
var now = time.Now().UnixMilli()
250254
for _, b := range c.storage {
@@ -262,8 +266,8 @@ func (c *MemoryCache[K, V]) Range(f func(K, V) bool) {
262266
}
263267
}
264268

265-
// Len 获取当前元素数量
266-
// Get the number of Elements
269+
// Len 快速获取当前缓存元素数量, 不做过期检查.
270+
// Quickly gets the current number of cached elements, without checking for expiration.
267271
func (c *MemoryCache[K, V]) Len() int {
268272
var num = 0
269273
for _, b := range c.storage {
@@ -282,8 +286,8 @@ type bucket[K comparable, V any] struct {
282286
List *queue[K, V]
283287
}
284288

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 {
287291
c.Lock()
288292
defer c.Unlock()
289293

@@ -302,12 +306,6 @@ func (c *bucket[K, V]) Delete(ele *Element[K, V], reason Reason) {
302306
ele.cb(ele, reason)
303307
}
304308

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-
311309
func (c *bucket[K, V]) UpdateTTL(ele *Element[K, V], expireAt int64) {
312310
c.Heap.UpdateTTL(ele, expireAt)
313311
c.List.MoveToBack(ele)

options.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func WithSwissTable(enabled bool) Option {
6767
}
6868
}
6969

70-
// WithLRU 是否开启LRU缓存驱逐算法. 默认为false
71-
// Whether to enable LRU cache eviction. Default is false
70+
// WithLRU 是否开启LRU缓存驱逐算法. 默认为true
71+
// Whether to enable LRU cache eviction. Default is true
7272
func WithLRU(enabled bool) Option {
7373
return func(c *config) {
7474
c.LRU = enabled
@@ -129,7 +129,7 @@ type config struct {
129129
// Whether to use swiss table, false by default.
130130
SwissTable bool
131131

132-
// 是否开启LRU缓存驱逐算法. 默认为false
133-
// Whether to enable LRU cache eviction. Default is false
132+
// 是否开启LRU缓存驱逐算法. 默认为true
133+
// Whether to enable LRU cache eviction. Default is true
134134
LRU bool
135135
}

queue.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ func (c *queue[K, V]) PushBack(ele *Element[K, V]) {
1818
return
1919
}
2020

21-
if c.length > 0 {
21+
c.length++
22+
if c.tail != nil {
2223
c.tail.next = ele
2324
ele.prev = c.tail
2425
c.tail = ele
25-
} else {
26-
c.head = ele
27-
c.tail = ele
26+
return
2827
}
29-
c.length++
28+
29+
c.head = ele
30+
c.tail = ele
3031
}
3132

3233
func (c *queue[K, V]) Pop() *Element[K, V] {

0 commit comments

Comments
 (0)