I'm using ServiceStack.Redis to execute commands on REDIS.
I would like to simply set value with expire and NX option (which is : set only if not exists).
SET resource_name my_value NX PX 30000
The problem is that ServiceStack.Redis client has only two methods:
client.SetValue() // <-- this one has Timespan expire argument
client.SetValueIfNotExists() // <-- this one doesn't
Is there a way to do this ?
I see a public bool SetValueIfNotExists(string key, string value, TimeSpan expireIn) in RedisClient.cs, line 183
Please mention what version are you using if you don't see it in your included package.
Related
I can't find anywhere online what is default TTL in Redis.
I know that I can set TTL for specific SET, but don't know what is default TTL.
Can someone tell me what default time to live is in Redis?
There is no default TTL. By default, keys are set to live forever.
The keys with no expiration time set will not expire.
If you mean TTL command specifically, starting with v2.8, it will return -2 if no EXPIRE value is set.
Edit:
Itamar Haber's comment is true, I recalled false: There is no such setting in redis config for a global TTL. So I deleted the part about that.
Edit2: Also see the link to the official docs about default expiration of keys here: https://redis.io/commands/expire#appendix-redis-expires
I suppose value set to '-1' by default which means forever.
127.0.0.1:6379> set datakey "my-data"
OK
127.0.0.1:6379> TTL datakey
(integer) -1
127.0.0.1:6379>
REDIS Docs says
Starting with Redis 2.8 the return value in case of error changed:
The command returns -2 if the key does not exist.
The command returns -1 if the key exists but has no associated expire.
The following used to work fine:
redis_client.setex(key, expiry_in_sec, value_json)
And now it suddenly returns:
value is not an integer or out of range
The issue is between the different redis clients.
When working with StrictRedis, the setex syntax is:
setex key, expiry, value
When working with Redis client, the setex syntax is:
setex key, value, expiry
our specific problem was that someone changed the redis client.
Redis will also return this error if the time value (or expiration time) is a float instead of an int.
In my case, using Redis in Python, I had to change the following:
Causes Error
ex = expiration_delta.total_seconds()
Fixed
ex = int(expiration_delta.total_seconds())
success = redis.set(name=redis_key, value=my_val, ex=ex, nx=True)
Note the ex argument to set() makes it work like setex.
I'm looking for a way to do a very simple TTL string in Redis:
So how do I do the equivalent of the following in StackExchange.Redis?
SETEX lolcat 10 "monorailcat"
I found KeyExpire, but that means every key I set needs two calls?
Oops. Never mind:
_Redis.StringSet( "lolcat", "monorailcat", TimeSpan.FromSeconds(10) );
I want to implement Absolute and Sliding Caching In Redis. Does anyone have any resource link then it will be helpful
Redis already have many commands for this :
EXPIRE : Set a timeout on key.
EXPIREAT : Same as previous but takes an absolute Unix timestamp (seconds since January 1, 1970).
TTL : Returns the remaining time to live of a key that has a timeout
One important thing you have to know about Expiration on Redis : the timeout value is cleared only when the key is removed or overwritten using SET or GETSET. All others commands (INCR, LPUSH, HMSET, ...) will never change the initial timeout.
Absolute expiration is a native feature of Redis using EXPIRE. To implement a sliding expiration you simply need to reset to timeout value after each command.
A basic way to do this could be
MULTI
GET MYKEY
EXPIRE MYKEY 60
EXEC
My redis server does not delete keys when the time-to-live reaches 0.
Here is a sample code:
redis-cli
>SET mykey "ismykey"
>EXPIRE mykey 20
#check TTL
>TTL mykey
>(integer) 17
> ...
>TTL mykey
>(integer) -1
#mykey chould have expired:
>EXISTS mykey
>(integer) 1
>#oh still there, check its value
>GET mykey
>"ismykey"
If i check the info return by redis, it says 0 keys were expired.
Any idea?
thanks.
Since you're doing a '...' it's hard to say for sure, but I'd say you're setting mykey during that part, which will effectively remove the expiration.
From the EXPIRE manual
The timeout is cleared only when the key is removed using the DEL
command or overwritten using the SET or GETSET commands
Also, regarding the -1 reply from TTL
Return value
Integer reply: TTL in seconds or -1 when key does not
exist or does not have a timeout.
EDIT: Note that this behaviour changed in Redis 2.8
Starting with Redis 2.8 the return value in case of error changed:
The command returns -2 if the key does not exist.
The command returns -1 if the key exists but has no associated expire.
In other words, if your key exists, it would seem to be persistent, ie not have any expiration set.
EDIT: It seems I can reproduce this if I create the key on a REDIS slave server, the slave will not delete the key without master input, since normally you would not create keys locally on a slave. Is this the case here?
However while the slaves connected to a master will not expire keys
independently (but will wait for the DEL coming from the master),
they'll still take the full state of the expires existing in the
dataset, so when a slave is elected to a master it will be able to
expire the keys independently, fully acting as a master.