Using KEYS and ARGV with Redis
We have already been using the keys and optional arguments that are accessible as the KEYS
and ARGV
Lua tables in our Lua server-side scripts in Redis. To illustrate this, we'll run ninth_script
that echoes back a Lua table with the KEYS
and ARGS
variables as members to the Redis client:
>>> ninth_script = """--[[ Returns all KEYS and ARGV as members of a Lua Table ]]-- return {KEYS[1], KEYS[2], ARGV[1], ARGV[2]}""" >>> keys_and_args = ["Airline:1", "Airline:2", "Singapore Airlines", "Southwest"] >>> datastore.eval(ninth_script, 2, *keys_and_args) [b'Airline:1', b'Airline:2', b'Singapore Airlines', b'Southwest']
We can refactor this script—now called tenth_script
—so that instead of requiring explicit keys for the Lua table, we can create a for
loop that iterates through all of the values in KEYS
and ARGV
and returns the resulting Lua table:
>>> tenth_script = """--[[ Demostrates creating a Lua table with both KEYS and ARGV...