Search code examples
javaredislettuce

Retrieving and deleting a key in single call from Redis server


For a use case, I want to delete a key upon retrieval from Redis server in a single call i.e. key is a one time use. I'm using lettuce library to connect to redis server.

Is there any specific configuration available in redis side or any specific lettuce API I can use? My sample code looks like below.

RedisAsyncCommands<String, String> command = notificationConnection.async();
CompletionStage<String> result = command.get(id)

Lettuce: 5.2.2

Redis: 5.0.8

thanks,

Ashok.


Solution

  • There is no single command to do it both. Your library may have a method to do it both but redis will receive two commands. If you want it to be atomic you either cover your commands it with multi/exec or use lua script. Here is the example usage of transactions with redis.

    127.0.0.1:6379> multi
    OK
    127.0.0.1:6379> get a
    QUEUED
    127.0.0.1:6379> del a
    QUEUED
    127.0.0.1:6379> exec
    1) "b" -> result of get
    2) (integer) 1 -> result of del