0
0
Redisquery~5 mins

Accessing keys and arguments in Redis

Choose your learning style9 modes available
Introduction
You use keys and arguments to tell Redis exactly which data to work with and what extra information to use.
When you want to get or set a specific value in Redis.
When writing a Redis script that needs to know which keys to access.
When passing extra information to a Redis command or script.
When you want to update multiple keys with some parameters.
When you want to control how a Redis command behaves using arguments.
Syntax
Redis
EVAL script numkeys key [key ...] arg [arg ...]
The first argument after the script is the number of keys you will pass.
Keys come first, then any extra arguments after the keys.
Examples
Gets the value of the key named 'mykey'.
Redis
EVAL "return redis.call('GET', KEYS[1])" 1 mykey
Returns the first key and the first argument passed.
Redis
EVAL "return {KEYS[1], ARGV[1]}" 1 key1 value1
Sets 'mykey' to 'newvalue'.
Redis
EVAL "redis.call('SET', KEYS[1], ARGV[1])" 1 mykey newvalue
Sample Program
This script returns the first two keys and the first two arguments passed to it.
Redis
EVAL "return {KEYS[1], KEYS[2], ARGV[1], ARGV[2]}" 2 keyA keyB arg1 arg2
OutputSuccess
Important Notes
Keys are accessed inside the script using KEYS array starting at index 1.
Arguments are accessed inside the script using ARGV array starting at index 1.
Always specify the correct number of keys after the script to avoid errors.
Summary
Keys tell Redis which data to use.
Arguments provide extra information to commands or scripts.
Use KEYS and ARGV arrays inside scripts to access keys and arguments.