In redis, how do I delete one key and get its value at the same time
Asked Answered
G

3

9

In redis, how do I delete one key and get its value at the same time, and, most important, it is executed in one command so it is thread safe.

Glyph answered 5/12, 2017 at 8:47 Comment(1)
I think you'll have to implement something of your own. rediscookbook.org/get_and_delete.htmlWaggish
C
8

There's no single command. You can either write a Lua script or execute a transaction. A transaction would simply look like:

127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> GET foo
QUEUED
127.0.0.1:6379> DEL foo
QUEUED
127.0.0.1:6379> EXEC
1) "bar"
2) (integer) 1

Using a Lua script

127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> EVAL "local x = redis.call('GET', KEYS[1]); redis.call('DEL', KEYS[1]); return x" 1 foo
"bar"
127.0.0.1:6379> GET foo
(nil)

Both operate the same, but with Lua script, the script can be cached and there's no need to repeat the whole code the next time you want to call it. We can use SCRIPT LOAD that caches the scripts and returns a unique id of it to be used as a function name for subsequent calls (Most clients abstract this transparently) e.g.

127.0.0.1:6379> SCRIPT LOAD "local x = redis.call('GET', KEYS[1]); redis.call('DEL', KEYS[1]); return x"
"89d675c84cf3bd6b3b38ab96fec7c7fb2386e4b7"

127.0.0.1:6379> SET foo bar
OK

# Now we use the returned SHA of the script to call it without parsing it again:
127.0.0.1:6379> EVALSHA 89d675c84cf3bd6b3b38ab96fec7c7fb2386e4b7 1 foo
"bar"
Corvus answered 5/12, 2017 at 9:50 Comment(3)
Alternatively, one could develop a module for that ;) (upvoted oc).Rapallo
In transaction, will it happen another command like decr executed between GET and DEL command?Sweetening
@FridayLi it cannot happen during the transaction execution, only while you are buffering the transaction.Corvus
M
12

Update 2021: as of redis v 6.2.0, you can use GETDEL

Manatarms answered 7/3, 2021 at 12:44 Comment(0)
C
8

There's no single command. You can either write a Lua script or execute a transaction. A transaction would simply look like:

127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> GET foo
QUEUED
127.0.0.1:6379> DEL foo
QUEUED
127.0.0.1:6379> EXEC
1) "bar"
2) (integer) 1

Using a Lua script

127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> EVAL "local x = redis.call('GET', KEYS[1]); redis.call('DEL', KEYS[1]); return x" 1 foo
"bar"
127.0.0.1:6379> GET foo
(nil)

Both operate the same, but with Lua script, the script can be cached and there's no need to repeat the whole code the next time you want to call it. We can use SCRIPT LOAD that caches the scripts and returns a unique id of it to be used as a function name for subsequent calls (Most clients abstract this transparently) e.g.

127.0.0.1:6379> SCRIPT LOAD "local x = redis.call('GET', KEYS[1]); redis.call('DEL', KEYS[1]); return x"
"89d675c84cf3bd6b3b38ab96fec7c7fb2386e4b7"

127.0.0.1:6379> SET foo bar
OK

# Now we use the returned SHA of the script to call it without parsing it again:
127.0.0.1:6379> EVALSHA 89d675c84cf3bd6b3b38ab96fec7c7fb2386e4b7 1 foo
"bar"
Corvus answered 5/12, 2017 at 9:50 Comment(3)
Alternatively, one could develop a module for that ;) (upvoted oc).Rapallo
In transaction, will it happen another command like decr executed between GET and DEL command?Sweetening
@FridayLi it cannot happen during the transaction execution, only while you are buffering the transaction.Corvus
Z
1

Since Redis 6.2.0 you can use GETDEL command:

Get the value of the key and delete the key. This command is similar to GET, except it also deletes the key on success (if and only if the key's value type is a string).

Reference

Zito answered 22/8, 2023 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.