Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for PreviousTTL #121

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading