3
votes

I am trying to find all the keys and their values matching a specific pattern using py-redis and lua and here is my code

rc = redis.Redis(..)

rc.set('google:',100)
rc.set('google:3',200)
rc.set('google:2',3400)
rc.set('google',200)
rc.set('fb',300)


get_script = """
local value = redis.call('GET', KEYS[1])
return value
"""
get_values = rc.register_script(get_script)

print get_values(rc.keys(pattern='google:*'))

print get_values(keys=['google:'])
print get_values(keys=['google:*'])

The output that I am getting is

100
100
None

First of all I do not get why I am getting None for the last print statement. My original purpose is to get all the keys ( and their values) matching the pattern but I am only getting the first key

1
perhaps it takes a regex. try google:.* - Ionut Hulub
This gives me the following error ResponseError: @user_script: 2: Lua redis() command arguments must be strings or integers - MARK

1 Answers

3
votes

I think that I have found what I was missing

Instead of GET I should have passed KEYS as first argument of the initial redis.call and then iterate over the keys to get the values

get_script = """
local keys = (redis.call('keys', ARGV[1]))
local values={}  

for i,key in ipairs(keys) do 
    local val = redis.call('GET', key)
    values[i]=val
    i=i+1
end

return values
"""
get_values = rc.register_script(get_script)

print get_values(args=['google:*'])