jiamo
jiamo

Reputation: 1456

keys and mget is not atomic?

I have a case:

# a = r.keys("test_*") ; a 
{'test_1': '1', 'test_2': '2'}
# b = r.mget(a) ; b
['1', ''2]

But test_1 may timeout when mget. So How can I make keys and mget as atomic step to make

 dict(zip(keys, r.mget(keys))

always get what I want?

Upvotes: 0

Views: 237

Answers (1)

for_stack
for_stack

Reputation: 22936

In order to make these two commands running atomically, you can use lua scripting.

--atomic.lua
local keys = redis.call('keys', '*')
local result = {}
for idx, key in ipairs(keys) do
    table.insert(result, key)   -- insert key
    table.insert(result, redis.call('get', key))    -- insert value
end
return result

However, it's almost always a BAD idea to use the KEYS command, since it might block Redis for a long time.

Upvotes: 2

Related Questions