w3resource

Redis Scripting: EVAL script numkeys key [key ...] arg [arg ...]

Redis EVAL command

Redis EVAL command is used to evaluate scripts using the Lua interpreter. The first argument of EVAL is a Lua 5.1 script and the script does not need to define a Lua function. It is just a Lua program that will run in the context of the Redis server. The second argument of EVAL is the number of arguments that follows the script that represents Redis key names. These arguments can be accessed by Lua using the KEYS global variable in the form of a one-based array (so KEYS[1], KEYS[2], ...).

Here are some common uses and scenarios for the "EVAL" command:

  • Execute Lua Scripts: Executes Lua scripts on the Redis server, allowing for complex operations and custom logic to be performed directly on the server.
  • Atomic Operations: Enables atomic execution of multiple Redis commands in a single script, ensuring that operations are executed as a single unit.
  • Custom Logic: Supports implementation of custom logic and data manipulation that is not possible with standard Redis commands.
  • Performance Optimization: Reduces the need for multiple round-trips between the client and server by bundling multiple operations into a single script.

Syntax:

EVAL script numkeys key [key ...] arg [arg ...] 

Available since

2.6.0.

Example:

redis 127.0.0.1:6379> eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second
1) "key1"
2) "key2"
3) "first"
4) "second"

Previous: Redis Transactions WATCH
Next: EVALSHA



Follow us on Facebook and Twitter for latest update.