I’m trying to get all the key value pairs for a given prefix from Redis, without making many network requests using Rust. The answers so far suggest using bash or making multiple requests.
I’m trying to get all the key value pairs for a given prefix from Redis, without making many network requests using Rust. The answers so far suggest using bash or making multiple requests.
2
Answers
The best method I've found is to use a lua script to construct the response. The example below is not the fastest since we can use
EVALSHA
to reduce our request size.Code:
Docs: https://docs.rs/redis/latest/redis/struct.Cmd.html#method.arg
The bottom line is that Redis doesn’t do this. You can get all the keys for a pattern using the KEYS or SCAN commands. Then you can GET the keys that are returned. And you can write a Lua script or use JavaScript to write a function to do this for you.
However, this is neither very convenient nor is it particularly fast, especially if you have a lot of data. And, the KEYS command is problematic as it block the main Redis thread while it runs. Furthermore, if you are running a cluster, this gets more complex as the keys you want to find are going to be on multiple servers.