mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-01 19:07:38 +03:00
Add freelru.GetWithLifetimeNoExpire
This commit is contained in:
parent
ea0ac932ae
commit
ea82ac275f
4 changed files with 41 additions and 0 deletions
|
@ -49,6 +49,8 @@ type Cache[K comparable, V comparable] interface {
|
||||||
|
|
||||||
GetWithLifetime(key K) (V, time.Time, bool)
|
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
|
// GetAndRefresh returns the value associated with the key, setting it as the most
|
||||||
// recently used item.
|
// recently used item.
|
||||||
// The lifetime of the found cache item is refreshed, even if it was already expired.
|
// The lifetime of the found cache item is refreshed, even if it was already expired.
|
||||||
|
|
|
@ -500,6 +500,24 @@ func (lru *LRU[K, V]) getWithLifetime(hash uint32, key K) (value V, lifetime tim
|
||||||
return
|
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
|
// GetAndRefresh returns the value associated with the key, setting it as the most
|
||||||
// recently used item.
|
// recently used item.
|
||||||
// The lifetime of the found cache item is refreshed, even if it was already expired.
|
// The lifetime of the found cache item is refreshed, even if it was already expired.
|
||||||
|
|
|
@ -187,6 +187,17 @@ func (lru *ShardedLRU[K, V]) GetWithLifetime(key K) (value V, lifetime time.Time
|
||||||
return
|
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
|
// GetAndRefresh returns the value associated with the key, setting it as the most
|
||||||
// recently used item.
|
// recently used item.
|
||||||
// The lifetime of the found cache item is refreshed, even if it was already expired.
|
// The lifetime of the found cache item is refreshed, even if it was already expired.
|
||||||
|
|
|
@ -108,6 +108,16 @@ func (lru *SyncedLRU[K, V]) GetWithLifetime(key K) (value V, lifetime time.Time,
|
||||||
return
|
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
|
// GetAndRefresh returns the value associated with the key, setting it as the most
|
||||||
// recently used item.
|
// recently used item.
|
||||||
// The lifetime of the found cache item is refreshed, even if it was already expired.
|
// The lifetime of the found cache item is refreshed, even if it was already expired.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue