Skip to content

Does .insert reset the TTL time? #410

Answered by tatsuya6502
tqwewe asked this question in Q&A
Discussion options

You must be logged in to vote

Thank you for using moka.

If I call .insert on an item that already exists, will this item's TTL still be updated to the current time + ttl?

Yes, the item's TTL must be updated.

Here is an unit test: https://github.com/moka-rs/moka/blob/v0.12.5/src/sync/cache.rs#L2470-L2491

  1. Create a cache with TTL 10 secs.
  2. Insert a key "b" with value "bob".
  3. Wait for 5 secs.
  4. Insert the same key "b" with value "bill".
  5. Wait for 5 secs.
  6. Get the key "b".
    • → "b" with "bill" still exists. (10 secs past from the initial insertion, but only 5 secs past from the update)
        let mut cache = Cache::builder()
            .max_capacity(100)
            .time_to_live(Duration::from_secs(10))
            .evict…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tqwewe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants