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

Set doesn't reset TTL if the key already exists #136

Open
vsavicks opened this issue May 9, 2024 · 1 comment
Open

Set doesn't reset TTL if the key already exists #136

vsavicks opened this issue May 9, 2024 · 1 comment
Labels

Comments

@vsavicks
Copy link

vsavicks commented May 9, 2024

According to the docs of the Set() method:

If an item associated with the provided key already exists, the new item overwrites the existing one.

However, if I try to set an item that already exists but has an expired TTL and delete all expired, the newly set item also gets deleted:

package main

import (
	"fmt"
	"time"

	"github.com/jellydator/ttlcache/v3"
)

func main() {
	cache := ttlcache.New[string](
		ttlcache.WithTTL[string, string](1 * time.Second),
	)
	cache.Set("foo", "bar", ttlcache.DefaultTTL)
	fmt.Printf("first: %s\n", cache.Keys())

	time.Sleep(2 * time.Second)
	fmt.Printf("after 2s: %s\n", cache.Keys())

	cache.DeleteExpired()
	fmt.Printf("after delete: %s\n", cache.Keys())

	cache.Set("baz", "qux", ttlcache.DefaultTTL)
	fmt.Printf("new: %s\n", cache.Keys())

	time.Sleep(2 * time.Second)
	fmt.Printf("after 2s: %s\n", cache.Keys())

	cache.Set("baz", "qum", ttlcache.DefaultTTL)
	cache.DeleteExpired()
	fmt.Printf("after set and delete: %s\n", cache.Keys())
}

Output:

first: [foo]
after 2s: [foo]
after delete: []
new: [baz]
after 2s: [baz]
after set and delete: []

I would expect the replaced item to have new TTL and not get deleted by DeleteExpired() call.

@swithek swithek added the bug label May 11, 2024
@swithek
Copy link
Contributor

swithek commented May 11, 2024

Thanks for reporting this @vsavicks. We'll try to have it fixed soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants