From f0e70276b93a49aa672dd247f4dddfc5f208d07f Mon Sep 17 00:00:00 2001 From: Yasin Bahtiyar Date: Thu, 31 Aug 2023 08:52:43 +0200 Subject: [PATCH] set missing Expiration field on evicted items (#345) 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 --- ttl.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ttl.go b/ttl.go index 337976ad..4e5c0248 100644 --- a/ttl.go +++ b/ttl.go @@ -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 } @@ -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, }) } }