Skip to content

Have you considered a get_with_ttl or a get_and_refresh function? #435

Description

@juancgalvis

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions