Skip to content

Commit

Permalink
Delete expired items before setting
Browse files Browse the repository at this point in the history
Check if an item exists and it has expired before setting so that
onEvicted is actually called.

Fixes #48.
  • Loading branch information
jaimem88 committed Oct 13, 2020
1 parent 46f4078 commit a5ae76b
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
e = time.Now().Add(d).UnixNano()
}
c.mu.Lock()
if i, ok := c.items[k]; ok && i.Expired() {
// delete if expired so .onEvicted is called
c.delete(k)
}

c.items[k] = Item{
Object: x,
Expiration: e,
Expand Down

0 comments on commit a5ae76b

Please sign in to comment.