Equivalent of SETEX for GET and TTL

2.2k Views Asked by At

SETEX is a useful Redis command whereby one can set the value and expiry of a key in a single, atomic operation.

Is there an equivalent operation that atomically enables a person to retrieve the key's value and ttl? I know I can do it in a pipeline as well, but I'm asking whether something elegant like SETEX exists. If it matters, I'm using Redis 2.8.4.

2

There are 2 best solutions below

4
On BEST ANSWER

AFAIK, there's no such command. However, you can wrap GET and TTL into a Lua script, to get both value and ttl in a single atomic call.

Also, you CANNOT achieve that with pipeline. Because Redis DOES NOT guarantee that commands in pipeline are running atomically.

0
On

You could use MULTI/EXEC to make sure some commands being in a transaction. pipeline just save the network transport time(rtt), because it will send the request in a batch and recieve reply in a batch also.

You could refer Redis Transaction and pipelining for more details.