From ea82ac275feb97afaf073cfd9118e5528a6afbf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 24 Feb 2025 18:25:10 +0800 Subject: [PATCH] Add freelru.GetWithLifetimeNoExpire --- contrab/freelru/cache.go | 2 ++ contrab/freelru/lru.go | 18 ++++++++++++++++++ contrab/freelru/shardedlru.go | 11 +++++++++++ contrab/freelru/syncedlru.go | 10 ++++++++++ 4 files changed, 41 insertions(+) diff --git a/contrab/freelru/cache.go b/contrab/freelru/cache.go index e1877fb..22488e0 100644 --- a/contrab/freelru/cache.go +++ b/contrab/freelru/cache.go @@ -49,6 +49,8 @@ type Cache[K comparable, V comparable] interface { GetWithLifetime(key K) (V, time.Time, bool) + GetWithLifetimeNoExpire(key K) (V, time.Time, bool) + // GetAndRefresh returns the value associated with the key, setting it as the most // recently used item. // The lifetime of the found cache item is refreshed, even if it was already expired. diff --git a/contrab/freelru/lru.go b/contrab/freelru/lru.go index 6c31857..055057c 100644 --- a/contrab/freelru/lru.go +++ b/contrab/freelru/lru.go @@ -500,6 +500,24 @@ func (lru *LRU[K, V]) getWithLifetime(hash uint32, key K) (value V, lifetime tim return } +func (lru *LRU[K, V]) GetWithLifetimeNoExpire(key K) (value V, lifetime time.Time, ok bool) { + return lru.getWithLifetimeNoExpire(lru.hash(key), key) +} + +func (lru *LRU[K, V]) getWithLifetimeNoExpire(hash uint32, key K) (value V, lifetime time.Time, ok bool) { + if pos, ok := lru.findKeyNoExpire(hash, key); ok { + if pos != lru.head { + lru.unlinkElement(pos) + lru.setHead(pos) + } + lru.metrics.Hits++ + return lru.elements[pos].value, time.UnixMilli(lru.elements[pos].expire), ok + } + + lru.metrics.Misses++ + return +} + // GetAndRefresh returns the value associated with the key, setting it as the most // recently used item. // The lifetime of the found cache item is refreshed, even if it was already expired. diff --git a/contrab/freelru/shardedlru.go b/contrab/freelru/shardedlru.go index db97efa..fa325d7 100644 --- a/contrab/freelru/shardedlru.go +++ b/contrab/freelru/shardedlru.go @@ -187,6 +187,17 @@ func (lru *ShardedLRU[K, V]) GetWithLifetime(key K) (value V, lifetime time.Time return } +func (lru *ShardedLRU[K, V]) GetWithLifetimeNoExpire(key K) (value V, lifetime time.Time, ok bool) { + hash := lru.hash(key) + shard := (hash >> 16) & lru.mask + + lru.mus[shard].RLock() + value, lifetime, ok = lru.lrus[shard].getWithLifetimeNoExpire(hash, key) + lru.mus[shard].RUnlock() + + return +} + // GetAndRefresh returns the value associated with the key, setting it as the most // recently used item. // The lifetime of the found cache item is refreshed, even if it was already expired. diff --git a/contrab/freelru/syncedlru.go b/contrab/freelru/syncedlru.go index 38a854a..4364907 100644 --- a/contrab/freelru/syncedlru.go +++ b/contrab/freelru/syncedlru.go @@ -108,6 +108,16 @@ func (lru *SyncedLRU[K, V]) GetWithLifetime(key K) (value V, lifetime time.Time, return } +func (lru *SyncedLRU[K, V]) GetWithLifetimeNoExpire(key K) (value V, lifetime time.Time, ok bool) { + hash := lru.lru.hash(key) + + lru.mu.Lock() + value, lifetime, ok = lru.lru.getWithLifetimeNoExpire(hash, key) + lru.mu.Unlock() + + return +} + // GetAndRefresh returns the value associated with the key, setting it as the most // recently used item. // The lifetime of the found cache item is refreshed, even if it was already expired.