Skip to content

Commit

Permalink
More verbose size counter
Browse files Browse the repository at this point in the history
  • Loading branch information
thedae committed Jan 21, 2025
1 parent 91a3fb3 commit 2dcbca8
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

type LruCache struct {
maxItems uint64
maxSize uint64
current uint64
maxItems uint64
maxSize uint64
inUseSize uint64

mu sync.RWMutex

Expand Down Expand Up @@ -74,12 +74,12 @@ func (c *LruCache) Set(key string, resp []byte) {
// Add new item
c.items[key] = c.evictList.PushFront(&entry{key, resp})

c.current += uint64(len(resp))
c.inUseSize += uint64(len(resp))
// Verify size not exceeded and evict if necessary
for c.current > c.maxSize || uint64(len(c.items)) > c.maxItems {
for c.inUseSize > c.maxSize || uint64(len(c.items)) > c.maxItems {
if e := c.evictList.Back(); e != nil {
delete(c.items, e.Value.(*entry).key)
c.current -= uint64(len(e.Value.(*entry).value))
c.inUseSize -= uint64(len(e.Value.(*entry).value))
c.evictList.Remove(e)
}
}
Expand All @@ -91,7 +91,7 @@ func (c *LruCache) Delete(key string) {

if ent, ok := c.items[key]; ok {
delete(c.items, ent.Value.(*entry).key)
c.current -= uint64(len(ent.Value.(*entry).value))
c.inUseSize -= uint64(len(ent.Value.(*entry).value))
c.evictList.Remove(ent)
}

Expand Down

0 comments on commit 2dcbca8

Please sign in to comment.