The Redis PSETEX command sets a key to hold a given string value, and sets that key to time out after a given number of milliseconds.
Note: The SET command can do the same thing, and it’s possible that the PSETEX command may be deprecated at some point. Therefore, it’s probably a good idea to use SET instead of PSETEX if possible.
Syntax
The syntax for PSETEX goes like this:
PSETEX key milliseconds value
Where key is the key to set, milliseconds is the number of milliseconds before it expires, and value is the value to set.
It’s the equivalent of the following:
SET mykey value
EXPIRE mykey milliseconds
Which is basically the same as doing this:
SET mykey value PX milliseconds
Example
Here’s a simple example to demonstrate:
PSETEX lunch 30000 "Red Curry"
Result:
OK
Now, if I’m quick and use GET to return the value before the 30000 milliseconds is up, I can see the value:
GET lunch
Result:
"Red Curry"
But if I wait until after the 30000 milliseconds is up, I get a null result:
GET lunch
Result:
(nil)
As mentioned, we can do the same thing with the SET command, so it’s probably better to use that in case PSETEX is deprecated at some point.
There’s also a SETEX command that does the same thing as PSETEX, except that SETEX uses seconds.