This is next mini entry about small thing that makes me happy in terms of changes in redis. Nothing new :D but still nice :D.
SET, EXPIRE, CHECK, ???, REPEAT
While using redis there is very common task we do. It is SET
followed by EXPIRE
. We do this when we want to cache for some period of time some data.
1 2 |
|
This will set key user:1:token
to value "kuba"
and next set it to expire in 5 seconds. We can check Time to live on this key by using ttl command.
1
|
|
this will return number of seconds that this key will be valid for or an negative number if its not valid anymore.
SETEX
SETEX Introduced in redis 2.0.0 command, lets you do both things SET and EXPIRE in one go. How do we use it ? Its simple!
1
|
|
It is not key, value seconds! :D:D example usage:
1
|
|
This will set for 1500 seconds key “key:to:home” to value “4b234ferg34ret34rasd32rs”. Pretty easy thing to do.
PSETEX
Since Redis 2.6.0 we can use new command it is PSETEX
1
|
|
Will set “key:to:home” to expire in 15 seconds, its important to notice that TTL will give you back amount of time units! so if you did PSETEX it will me miliseconds and if it is SETEX it is in seconds!.
Cheers!