Skip to content

Commit

Permalink
removing of old cache entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Mancke committed Jul 11, 2016
1 parent 7a03887 commit ab8abaf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
33 changes: 32 additions & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (c *Cache) logEvery(d time.Duration) {
for {
select {
case <-time.After(d):
c.PurgeOldEntries()
c.calculateStats(d)
//c.RemoveOldEntries(d)
}
}
}
Expand Down Expand Up @@ -117,6 +117,10 @@ func (c *Cache) Set(key string, label string, sizeBytes int, cacheObject interfa
}
c.lock.Lock()
defer c.lock.Unlock()

// first remove, to have correct size counting
c.lruBackend.Remove(key)

c.currentSizeBytes += sizeBytes
c.lruBackend.Add(key, entry)

Expand All @@ -134,6 +138,33 @@ func (c *Cache) onEvicted(key, value interface{}) {
c.currentSizeBytes -= entry.size
}

// PurgeOldEntries removes all entries which are out of their ttl
func (c *Cache) PurgeOldEntries() {
c.lock.RLock()
keys := c.lruBackend.Keys()
c.lock.RUnlock()

purged := 0
for _, key := range keys {
c.lock.RLock()
e, found := c.lruBackend.Peek(key)
c.lock.RUnlock()

if found {
entry := e.(*CacheEntry)
if time.Since(entry.fetchTime) > c.maxAge {
c.lock.Lock()
c.lruBackend.Remove(key)
c.lock.Unlock()
purged++
}
}
}
logging.Logger.
WithFields(logrus.Fields(c.stats)).
Infof("purged %v out of %v cache entries", purged, len(keys))
}

// SizeByte returns the total memory consumption of the cache
func (c *Cache) SizeByte() int {
c.lock.RLock()
Expand Down
20 changes: 19 additions & 1 deletion cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func Test_Cache_MaxBytes(t *testing.T) {

// given a cache with max 1 mega byte, filled with 8 bytes
c := NewCache("my-cache", 100, 1, time.Hour)
c.Set("a", "", 400*1024, "a")
c.Set("a", "", 42*1024, "a")
c.Set("a", "", 400*1024, "a") // the same item only shoud count once, with the lastest bytes
c.Set("b", "", 400*1024, "b")
a.Equal(800*1024, c.SizeByte())
a.Equal(2, c.Len())
Expand Down Expand Up @@ -114,3 +115,20 @@ func Test_Cache_Stats(t *testing.T) {
a.Equal(1, c.stats["cache_misses"])
a.Equal(66, c.stats["cache_hit_ratio"])
}

func Test_Cache_PurgeOldEntries(t *testing.T) {
a := assert.New(t)

c := NewCache("my-cache", 100, 100, time.Millisecond)
c.Set("a", "", 1, "a")
c.Set("b", "", 1, "a")
c.Set("c", "", 1, "a")
time.Sleep(time.Millisecond)
c.Set("d", "", 42, "a")
c.Set("e", "", 42, "a")

c.PurgeOldEntries()

a.Equal(2, c.Len())
a.Equal(84, c.SizeByte())
}

0 comments on commit ab8abaf

Please sign in to comment.