This commit is contained in:
Neil Alexander 2022-03-01 16:28:39 +00:00
parent 2e2d40b7ba
commit 8e71afb828
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -126,8 +126,10 @@ func NewInMemoryLRUCachePartition(name string, mutable bool, maxEntries int, max
func (c *InMemoryLRUCachePartition) Set(key string, value interface{}) { func (c *InMemoryLRUCachePartition) Set(key string, value interface{}) {
if !c.mutable { if !c.mutable {
if peek, ok := c.lru.Peek(key); ok && peek != value { if peek, ok := c.lru.Peek(key); ok {
panic(fmt.Sprintf("invalid use of immutable cache tries to mutate existing value of %q", key)) if entry, ok := peek.(*inMemoryLRUCacheEntry); ok && entry.value != value {
panic(fmt.Sprintf("invalid use of immutable cache tries to mutate existing value of %q", key))
}
} }
} }
c.lru.Add(key, &inMemoryLRUCacheEntry{ c.lru.Add(key, &inMemoryLRUCacheEntry{
@ -149,12 +151,15 @@ func (c *InMemoryLRUCachePartition) Get(key string) (value interface{}, ok bool)
return nil, false return nil, false
} }
entry, ok := v.(*inMemoryLRUCacheEntry) entry, ok := v.(*inMemoryLRUCacheEntry)
if !ok { switch {
return nil, false case ok && c.maxAge == CacheNoMaxAge:
} return entry.value, ok // There's no maximum age policy
if c.maxAge > 0 && time.Since(entry.created) > c.maxAge { case ok && time.Since(entry.created) < c.maxAge:
return entry.value, ok // The value for the key isn't stale
default:
// Either the key was found and it was stale, or the key
// wasn't found at all
c.lru.Remove(key) c.lru.Remove(key)
return nil, false return nil, false
} }
return entry.value, true
} }