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

TTL and EXPIRE functions? #17

Closed
cohawk opened this issue Jan 18, 2018 · 3 comments
Closed

TTL and EXPIRE functions? #17

cohawk opened this issue Jan 18, 2018 · 3 comments

Comments

@cohawk
Copy link

cohawk commented Jan 18, 2018

Looking through the docs and codebase I do not see a way to return a key's current TTL - only set it. Unless I am missing it somewhere? (https://redis.io/commands/ttl)

Also along those lines it would be nice to have a Redis like EXPIRE function (https://redis.io/commands/expire) to refresh a key's TTL (get and set) or return 0/false if the key does not exist (or already expired).

cabol added a commit that referenced this issue Jan 28, 2018
@cabol
Copy link
Owner

cabol commented Jan 28, 2018

@cohawk I agree, it would be helpful to have these functions (especially the ttl to retrieve the remaining TTL for a specific key). But, I'm not sure to add them to the current cache interface. So my idea is:

  1. For the TTL which is more related to the Nebulex.Object module, I've added a function there in order to retrieve the remaining TTL. For Example:
MyCache.set("foo", "bar", ttl: 10)
:timer.sleep(5000)

remaining_ttl =
  "foo"
  |> MyCache.get(return: :object)
  |> Nebulex.Object.ttl()
  1. For the EXPIRE funtion, I think it is very easy to implement it using the current cache interface For example, using the update function we can implement something vey similar:
MyCache.update("foo", nil, &(&1), ttl: 5)
  • If the key doesn't exist, the update will return nil and nothing is stored in the cache (this is because we are passing as initial value nil).
  • If the key exists, the current value is kept and the other properties of the object like the ttl are updated (the function &(&1) is ensuring the same value)

Example:

remaining_ttl =
  "foo"
  |> MyCache.update(nil, &(&1), ttl: 5, return: :object) # this acts as EXPIRE function
  |> Nebulex.Object.ttl()

Let me know your thoughts!

cabol added a commit that referenced this issue Jan 28, 2018
@cohawk
Copy link
Author

cohawk commented Jan 30, 2018

@cabol, simple and beautiful. Passing the &(&1) function is much better than the get_and_update method that I was looking into.

I think I would still create wrapper functions with documentation (at least in my code) using your above examples and explanations, since I could see myself looking back at the Expire code block and not remembering exactly what it was doing.

But either way both functions are now possible. Thanks for your great work on the library!

@cabol
Copy link
Owner

cabol commented Jan 30, 2018

@cohawk thanks :) !!

@cabol cabol closed this as completed Jan 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants