Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all the values matching a key pattern from redis through Lua

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

like image 718
MARK Avatar asked Oct 22 '25 15:10

MARK


1 Answers

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:*'])
like image 163
MARK Avatar answered Oct 25 '25 04:10

MARK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!