I'm using redis (with Java Redis) and I want to use a HASH with a lot of keys and some data (binary stored) in each key I want to iterate by the keys of the map, while not blocking the server; because a lot of processes access this server.
Because of this, I don't want to use the HKEYS command.
And I would like to use the HSCAN command, or a similar method. Because I only want the keys in this step of my operations.
But as the documentation explains, the HSCAN method returns:
- The cursor
- An array of pair of key-values
(see scan, and hscan (not many info, and refers to the last link ))
As I said, I want to recieve only the keys; not the values. But the HSCAN method returns both, and I'm afraid it can hurt the performance of the server and my application.
I've been searching the documentation and forums, and found no response. But if anyone knows better, please let me know.
It bugs me a little to know that SCAN method works like I want but HSCAN not.... wonder why.
Thanks
While HSCAN
indeed returns the field-value pairs in a Hash, you can transform the output to filter the values with a server-side Lua script (see the EVAL
for details). This will reduce the network usage and client-side load, at the price of making the server work (a little) harder.
The following script provides a possible implementation:
local key = KEYS[1]
local cursor = ARGV[1]
local res = redis.call('HSCAN', key, cursor)
local rep = { res[1], {} }
for i=1, #res[2], 2 do
table.insert(rep[2], res[2][i])
end
return rep