Redis Cache - Get the initial timeout set - redis

In Redis Cache, is it possible to retrieve the original TimeOut that was set on a key? I know that there is a way to retrieve the pending TimeToLive of any key but I want the original value that was set while creating the key.

No, Redis doesn't store the original TTL for keys. It would be interesting to understand the use case that requires this.
You could, however, use a Sorted Set to keep track of the initial TTLs. The idea is that after each call to EXPIRE, call ZADD on that set with the member being the key's name. The score should be a decimal, where the part before the decimal point is the expiration timestamp and the fractional part is the TTL (padded with 0s according to your max TTL).
To retrieve the initial TTL, call ZSCORE on the set with the key's name and extract the part after the decimal point.
Note that by taking this approach you'll have to do some housekeeping, namely removing expired members from the set. To do that, periodically call ZREMBYSCORE from -inf to the current timestamp.

Related

Redis: How to update a key but do not change it's expiration time?

Using Redis as cache service to cache some non-important data, and there is a case that need to update the value without reset or override the expire time, is there any good way to resolve this problem?
I've searched and found follows 2 solutions
Using setrange command, since the value is a little more complex, so it is not good in this situation.
Get the ttl time then set it as the expiration time when update the value. it's seems a little more redundant.
Any good idea to resolve this problem?
You don't need to do any of those two things. You just need to use the KEEPTTL flag when you set your value.
Like this:
> set my_key this_is_my_value EX 60
This will set a value for a key with 60 seconds expiration.
Then, when you change the value and don't want to change the expiration of your key, just do:
> set my_key this_is_my_new_value KEEPTTL
More information on REDIS docs.
Another idea to so resolve this could be using INCRBY.
For this you have to do some steps.
Get the existing value. For example, 10.
Determine the update value.For example, 17 .
INCRBY the value of their difference 17-10. That means, 7
This won't change the ttl

Which redis data structure to use to store values as set with each value to have an expiry time

I've a use case where I need to store values for eg. SADD key *values
but I also want to persist the values only for certain duration say 1 day after which the specific value should get expired.
Please suggest how can this be achieved using redis.
Redis' expiration is implemented at key level, not inside the value. As an alternative, use a Sorted Set, with the score of each member being its expiration timestamp.
You will have to "expire" elements manually, so periodically call ZREMRANGEBYSCORE to remove all elements with a timestamp lower than now.

How to set expire timeout for redis set key only for the first key save?

I have case where i need to save values to redis SET structure under given key so i am using command from my code in the loop
SADD key value
EXPIRE KEY 100
However, i would like to set expiration time only on first save of the set key.
Is it possible to set expire time only at the moment of first set key creation ?
it is also should be noted that i can use EXISTS key call to redis to check if key exist and depending on that set expiration time or not - but this operation is not atomic.
To answer the question - no, there isn't such a command.
As you noted, this could be worked around with EXISTS. To address the atomicity (and save on network) requirement you can use a Lua script (see EVAL).
Redis 7.0 has a new option "NX -- Set expiry only when the key has no expiry". So, you could solve the problem with EXPIRE KEY 100 NX.
However, that it doesn't matter if you call EXPIRE KEY 100 1 or, let's say, 5 times. The key will expire after 100 seconds, thus checking EXISTS key is also an option.

Set a value to a key with ttl

Is it possible to setnx a key with a value and a ttl in single command in redis
I am trying to implement locking in redis and http://redis.io/commands/hsetnx seems like the best way to do that. It is atomic and returns 0 if a key is already present. Is it possible to HSETNX with TTL
e.g.
HSETNX myhash mykey "myvalue" 10
#and key expires after 10 seconds, and a subsequent HSETNX after 10 seconds returns a value 1 i.e. it behaves as if mykey is not present in myhash
The main problem is that Redis have no support for fields expiration in hashmaps.
You can only expire the entire hashmap by calling EXPIRE on myhash.
So, you should reconsider using ordinary Redis strings instead of hashmaps, because they support SETEX operation.
It'll work fine unless you want to take advantage of using HGETALL, HKEYS or HVALS on your hashmap myhash:
SETEX mynamespace:mykey 10 "myvalue"
mynamespace is not a hashmap here, it simply a prefix, but in most cases it works just as hashmaps do. The only difference is that there is no efficient way to tell which keys are stored in the given namespace or to get them all with a single command.

How to know if key set to be expired or not in Redis?

As far as Redis do not allow to reSet expire date to key (because of nans with replication) I'd like to know is there any method to check if key set to be expired or not?
Thank you
Use the TTL command. If an expiration is set, it returns the number of seconds until the key expires; otherwise it returns -1.
I don't think checking for expiration date make much sense in Redis, though. I'd like to first suggest that you model it so you don't need to check for expiration date.
If you really need it, though, you can just use another key to store the expiration date for later retrieval via normal GET/SET.
Note that you can also check for EXPIRES manually in your client code, which might be a better solution.