Skip to content

Commit 2b727c9

Browse files
committed
Add freelru.GetWithLifetimeNoExpire
1 parent e45886b commit 2b727c9

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

contrab/freelru/cache.go

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type Cache[K comparable, V comparable] interface {
4949

5050
GetWithLifetime(key K) (V, time.Time, bool)
5151

52+
GetWithLifetimeNoExpire(key K) (V, time.Time, bool)
53+
5254
// GetAndRefresh returns the value associated with the key, setting it as the most
5355
// recently used item.
5456
// The lifetime of the found cache item is refreshed, even if it was already expired.

contrab/freelru/lru.go

+18
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,24 @@ func (lru *LRU[K, V]) getWithLifetime(hash uint32, key K) (value V, lifetime tim
500500
return
501501
}
502502

503+
func (lru *LRU[K, V]) GetWithLifetimeNoExpire(key K) (value V, lifetime time.Time, ok bool) {
504+
return lru.getWithLifetimeNoExpire(lru.hash(key), key)
505+
}
506+
507+
func (lru *LRU[K, V]) getWithLifetimeNoExpire(hash uint32, key K) (value V, lifetime time.Time, ok bool) {
508+
if pos, ok := lru.findKeyNoExpire(hash, key); ok {
509+
if pos != lru.head {
510+
lru.unlinkElement(pos)
511+
lru.setHead(pos)
512+
}
513+
lru.metrics.Hits++
514+
return lru.elements[pos].value, time.UnixMilli(lru.elements[pos].expire), ok
515+
}
516+
517+
lru.metrics.Misses++
518+
return
519+
}
520+
503521
// GetAndRefresh returns the value associated with the key, setting it as the most
504522
// recently used item.
505523
// The lifetime of the found cache item is refreshed, even if it was already expired.

contrab/freelru/shardedlru.go

+11
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ func (lru *ShardedLRU[K, V]) GetWithLifetime(key K) (value V, lifetime time.Time
187187
return
188188
}
189189

190+
func (lru *ShardedLRU[K, V]) GetWithLifetimeNoExpire(key K) (value V, lifetime time.Time, ok bool) {
191+
hash := lru.hash(key)
192+
shard := (hash >> 16) & lru.mask
193+
194+
lru.mus[shard].RLock()
195+
value, lifetime, ok = lru.lrus[shard].getWithLifetimeNoExpire(hash, key)
196+
lru.mus[shard].RUnlock()
197+
198+
return
199+
}
200+
190201
// GetAndRefresh returns the value associated with the key, setting it as the most
191202
// recently used item.
192203
// The lifetime of the found cache item is refreshed, even if it was already expired.

contrab/freelru/syncedlru.go

+10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ func (lru *SyncedLRU[K, V]) GetWithLifetime(key K) (value V, lifetime time.Time,
108108
return
109109
}
110110

111+
func (lru *SyncedLRU[K, V]) GetWithLifetimeNoExpire(key K) (value V, lifetime time.Time, ok bool) {
112+
hash := lru.lru.hash(key)
113+
114+
lru.mu.Lock()
115+
value, lifetime, ok = lru.lru.getWithLifetimeNoExpire(hash, key)
116+
lru.mu.Unlock()
117+
118+
return
119+
}
120+
111121
// GetAndRefresh returns the value associated with the key, setting it as the most
112122
// recently used item.
113123
// The lifetime of the found cache item is refreshed, even if it was already expired.

0 commit comments

Comments
 (0)