cache icon indicating copy to clipboard operation
cache copied to clipboard

entry can not stay longer than ttl to cache:start_link(...)

Open leoliu opened this issue 4 years ago • 3 comments

For example,

{ok, _} = cache:start_link(my_cache, [{n, 10}, {ttl, 60}]).
cache:put(my_cache, x, 100, 120).

cache:lookup(my_cache, x) will return undefined after about 54 seconds. Is this a bug?

leoliu avatar Aug 09 '20 00:08 leoliu

cache:put(my_cache, x, 100, 120) request 120 sec TTL but TTL cannot be longer than cache level TTL, which is 60 sec in your case. I think it is more issue with docs and explaining how TTL works in depth.

fogfish avatar Aug 10 '20 10:08 fogfish

Indeed I completely misunderstood how TTL worked here.

leoliu avatar Aug 10 '20 14:08 leoliu

Thank you for you input! I'll update the doc.

Meanwhile:

  • Cache is sliced on X segments.
  • Cache applies eviction and quota policies at segment level. The oldest ETS table is destroyed and new one is created when quota or TTL criteria are exceeded.
  • ttl=60 and n=10 causes 10 segments, with 6 sec life time. Every 6 sec a segment is evicted.
  • when you put element to cache, it looks up a corresponding segment that roughly approximates TTL
  • if you can item with TTL great than cache TTL it stored into the latest segment.
  • In you case, the latest segment is going to be expired in 60 - 54 sec.

fogfish avatar Aug 10 '20 14:08 fogfish