Lam Lee
Lam Lee

Reputation: 179

How to get all hashmap match with prefix in Redis?

I have redis data with the format below:

HMSET name:1 key "value"
HMSET name:2 key "value2"
HMSET name:3 key "value3"
HMSET name:a key "valuea"

How to get all hashmap with prefix is "name:" the same result below:

name:1 key "value"
name:2 key "value2"
name:3 key "value3"

Upvotes: 0

Views: 1752

Answers (1)

LeoMurillo
LeoMurillo

Reputation: 6764

You want to use SCAN and not KEYS, as KEYS may block your server for too long.

You want to filter by hashmap type. See more in How can I get all of the sets in redis?

Then, you want to append the field-values to the response. For that you need Lua Scripting.

This script should get you started:

local result = redis.call('SCAN', ARGV[1], 'MATCH', ARGV[2])
local filtered = {}
for _,key in ipairs(result[2]) do
    if redis.call('TYPE', key).ok == ARGV[3] then
        local keyWithHash = {}
        keyWithHash[1] = key
        keyWithHash[2] = redis.call('HGETALL', key)
        table.insert(filtered, keyWithHash)
    end
end
result[2] = filtered
return result

If you are using Redis 6 you can optimize it by making the SCAN use the TYPE option instead of filtering one by one by calling TYPE command.

Use as:

EVAL "local result = redis.call('SCAN', ARGV[1], 'MATCH', ARGV[2]) local filtered = {} for _,key in ipairs(result[2]) do     if redis.call('TYPE', key).ok == ARGV[3] then      local keyWithHash = {}      keyWithHash[1] = key        keyWithHash[2] = redis.call('HGETALL', key)         table.insert(filtered, keyWithHash)     end end result[2] = filtered return result" 0 0 "name:*" hash

Pre-test:

HMSET name:1 key "value"
HMSET name:2 key "value2"
HMSET name:3 key "value3"
HMSET name:a key "valuea"
SET name:notAhash "asdasd"

Test:

> EVAL "local result ... return result" 0 0 "name:*" hash
1) "0"
2) 1) 1) "name:a"
      2) 1) "key"
         2) "valuea"
   2) 1) "name:3"
      2) 1) "key"
         2) "value3"
   3) 1) "name:2"
      2) 1) "key"
         2) "value2"
   4) 1) "name:1"
      2) 1) "key"
         2) "value"

Upvotes: 3

Related Questions