Skip to content

Commit

Permalink
set missing Expiration field on evicted items (#345)
Browse files Browse the repository at this point in the history
On our use case we need to rely on expiration timestamps but we realized
that they return zero timestamps. Our assumption is that this is due to
a bug and not by design.

## Problem
When `OnEvict` callback is executed `Item` does not have `Expiration`
field set. Considering it is a public field this needs to be set and
available.

## Solution
Initialize `Expiration` field before calling the `onEvict` callback
  • Loading branch information
0x1ee7 committed Aug 31, 2023
1 parent e8dc5b0 commit f0e7027
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions ttl.go
Expand Up @@ -127,8 +127,9 @@ func (m *expirationMap) cleanup(store store, policy policy, onEvict itemCallback
m.Unlock()

for key, conflict := range keys {
expr := store.Expiration(key)
// Sanity check. Verify that the store agrees that this key is expired.
if store.Expiration(key).After(now) {
if expr.After(now) {
continue
}

Expand All @@ -138,9 +139,10 @@ func (m *expirationMap) cleanup(store store, policy policy, onEvict itemCallback

if onEvict != nil {
onEvict(&Item{Key: key,
Conflict: conflict,
Value: value,
Cost: cost,
Conflict: conflict,
Value: value,
Cost: cost,
Expiration: expr,
})
}
}
Expand Down

0 comments on commit f0e7027

Please sign in to comment.