I have a scenario where I have items with a TTL (Time To Live). Each item has a TTL of 5 minutes when inserted. Every time I read the item, if it's cached, I also get the TTL, and if the TTL is less than 30 seconds, I perform a refresh. In this case, I read both the value and the TTL. Would it be optimal to have both values in a new function, Cachex.get_with_ttl?
# The put
Cachex.put(:my_cache, "key", "value", expire: :timer.minutes(5))
# In another code location after unspecified time
case Cachex.get(:my_cache, "key") do
{:ok, value} when value != nil ->
{:ok, ttl} = Cachex.ttl(:my_cache, "key")
if ttl < 30_000 do
Cachex.refresh(:my_cache, "key")
end
value
_other ->
:not_found
end
The key and value are hardcoded to show a sample.
or even implement a function with the logic to perform the refresh:
get_and_refresh(:my_cache, "key", min_ttl: 30_000)
I have a scenario where I have items with a TTL (Time To Live). Each item has a TTL of 5 minutes when inserted. Every time I read the item, if it's cached, I also get the TTL, and if the TTL is less than 30 seconds, I perform a refresh. In this case, I read both the value and the TTL. Would it be optimal to have both values in a new function,
Cachex.get_with_ttl?The key and value are hardcoded to show a sample.
or even implement a function with the logic to perform the refresh: