Skip to content

Commit

Permalink
Add Support for PreviousTTL (jellydator#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivamKumar2002 authored Feb 19, 2024
1 parent a91502b commit 9766960
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
c.evict(EvictionReasonCapacityReached, c.items.lru.Back())
}

if ttl == PreviousOrDefaultTTL {
ttl = c.options.ttl
}

// create a new item
item := newItem(key, value, ttl, c.options.enableVersionTracking)
elem = c.items.lru.PushFront(item)
Expand Down
22 changes: 22 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ func Test_Cache_set(t *testing.T) {
Key: existingKey,
TTL: DefaultTTL,
},
"Set with existing key and PreviousOrDefaultTTL": {
Key: existingKey,
TTL: PreviousOrDefaultTTL,
},
"Set with new key and eviction caused by small capacity": {
Capacity: 3,
Key: newKey,
Expand Down Expand Up @@ -232,6 +236,14 @@ func Test_Cache_set(t *testing.T) {
},
ExpectFns: true,
},
"Set with new key and PreviousOrDefaultTTL": {
Key: newKey,
TTL: PreviousOrDefaultTTL,
Metrics: Metrics{
Insertions: 1,
},
ExpectFns: true,
},
}

for cn, c := range cc {
Expand All @@ -245,6 +257,9 @@ func Test_Cache_set(t *testing.T) {
evictionFnsCalls int
)

// calculated based on how addToCache sets ttl
existingKeyTTL := time.Hour + time.Minute

cache := prepCache(time.Hour, evictedKey, existingKey, "test3")
cache.options.capacity = c.Capacity
cache.options.ttl = time.Minute * 20
Expand Down Expand Up @@ -295,6 +310,13 @@ func Test_Cache_set(t *testing.T) {
assert.Equal(t, c.TTL, item.ttl)
assert.WithinDuration(t, time.Now(), item.expiresAt, c.TTL)
assert.Equal(t, c.Key, cache.items.expQueue[0].Value.(*Item[string, string]).key)
case c.TTL == PreviousOrDefaultTTL:
expectedTTL := cache.options.ttl
if c.Key == existingKey {
expectedTTL = existingKeyTTL
}
assert.Equal(t, expectedTTL, item.ttl)
assert.WithinDuration(t, time.Now(), item.expiresAt, expectedTTL)
default:
assert.Equal(t, c.TTL, item.ttl)
assert.Zero(t, item.expiresAt)
Expand Down
20 changes: 15 additions & 5 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const (
// NoTTL indicates that an item should never expire.
NoTTL time.Duration = -1

// PreviousOrDefaultTTL indicates that existing TTL of item should be used
// default TTL will be used as fallback if item doesn't exist
PreviousOrDefaultTTL time.Duration = -2

// DefaultTTL indicates that the default TTL value of the cache
// instance should be used.
DefaultTTL time.Duration = 0
Expand Down Expand Up @@ -58,17 +62,23 @@ func (item *Item[K, V]) update(value V, ttl time.Duration) {
defer item.mu.Unlock()

item.value = value

// update version if enabled
if item.version > -1 {
item.version++
}

// no need to update ttl or expiry in this case
if ttl == PreviousOrDefaultTTL {
return
}

item.ttl = ttl

// reset expiration timestamp because the new TTL may be
// 0 or below
item.expiresAt = time.Time{}
item.touchUnsafe()

// update version if enabled
if item.version > -1 {
item.version++
}
}

// touch updates the item's expiration timestamp.
Expand Down
8 changes: 7 additions & 1 deletion item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ func Test_Item_update(t *testing.T) {
assert.Equal(t, int64(1), item.version)
assert.WithinDuration(t, time.Now().Add(time.Hour), item.expiresAt, time.Minute)

item.update("previous ttl", PreviousOrDefaultTTL)
assert.Equal(t, "previous ttl", item.value)
assert.Equal(t, time.Hour, item.ttl)
assert.Equal(t, int64(2), item.version)
assert.WithinDuration(t, time.Now().Add(time.Hour), item.expiresAt, time.Minute)

item.update("hi", NoTTL)
assert.Equal(t, "hi", item.value)
assert.Equal(t, NoTTL, item.ttl)
assert.Equal(t, int64(2), item.version)
assert.Equal(t, int64(3), item.version)
assert.Zero(t, item.expiresAt)
}

Expand Down

0 comments on commit 9766960

Please sign in to comment.