skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    let raw_kv: Vec<Option<(String, Vec<u8>)>> = tokio::time::timeout(
        timeout,
        redis::cmd("EVAL")
            .arg(
                // use KEYS instead of SCAN since EVAL is already blocking
                "
        local keys = redis.call('KEYS', ARGV[1]);
        if next(keys) == nil then return keys end;
        local out = {};
        for i=1,#keys do out[i] = {keys[i], redis.call('GET', keys[i])} end
        return out;
        ",
            )
            .arg(0) // no manually passed in keys
            .arg(pattern) // custom pattern to search for
            .query_async(&mut self.conn),
    )
    .await;
    

    Docs: https://docs.rs/redis/latest/redis/struct.Cmd.html#method.arg


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search