The KEYS table is pre-populated with all key name arguments provided to the script before its execution, whereas the ARGV table serves a similar purpose but for regular arguments. The execution context makes arguments available to the script through KEYS and ARGV global runtime variables. In the example above, both Hello and Parameterization! regular input arguments for the script.īecause the script doesn't touch any keys, we use the numerical argument 0 to specify there are no key name arguments. Scripts should never access keys with programmatically-generated names or based on the contents of data structures stored in the database.Īny input to the function that isn't the name of a key is a regular input argument. The script should only access keys whose names are given as input arguments. To ensure the correct execution of scripts, both in standalone and clustered deployments, all names of keys that a script accesses must be explicitly provided as input key arguments. The name of a key is a fundamental concept in Redis and is the basis for operating the Redis Cluster. While key names in Redis are just strings, unlike any other string values, these represent keys in the database. Redis> EVAL "return ARGV" 0 Parameterization!Īt this point, it is essential to understand the distinction Redis makes between input arguments that are names of keys and those that aren't. The following example demonstrates how to achieve the same effects as above, but via parameterization: redis> EVAL "return ARGV" 0 Hello Instead of having your application generate subtle variations of the same scripts, you can parametrize them and pass any arguments needed for to execute them. It is possible, although highly ill-advised, to have the application dynamically generate script source code per its needs.įor example, the application could send these two entirely different, but at the same time perfectly identical scripts: redis> EVAL "return 'Hello'" 0Īlthough this mode of operation isn't blocked by Redis, it is an anti-pattern due to script cache considerations (more on the topic below). In this example, we used the value 0 because we didn't provide the script with any arguments, whether the names of keys or not. The second argument is the number of arguments that follow the script's body, starting from the third argument, representing Redis key names. It is just a Lua program that will run in the Redis engine's context. The script doesn't need to include any definitions of Lua function. The first argument is a string that consists of the script's Lua source code. In this example, EVAL takes two arguments. Here's our first example: > EVAL "return 'Hello, scripting!'" 0 We'll start scripting with Redis by using the EVAL command. So all scripts may need to be reloaded by the application at any time if missing (after a server restart, fail-over to a replica, etc.).Īs of version 7.0, Redis Functions offer an alternative approach to programmability which allow the server itself to be extended with additional programmed logic. Please refer to the Redis Lua API Reference page for complete documentation.Īlthough the server executes them, Eval scripts are regarded as a part of the client-side application, which is why they're not named, versioned, or persisted. Presently, Redis supports a single scripting engine, the Lua 5.1 interpreter. Scripts are executed in Redis by an embedded execution engine. Such scripts can perform conditional updates across multiple keys, possibly combining several different data types atomically. Lua lets you run part of your application logic inside Redis. Enabling the composition of simple capabilities that are either missing from Redis or are too niche to a part of it. Blocking semantics that ensure the script's atomic execution.Data locality reduces overall latency and saves networking resources. Providing locality by executing logic where data lives.Scripting offers several properties that can be valuable in many cases. These semantics mean that all of the script's effects either have yet to happen or had already happened. While executing the script, all server activities are blocked during its entire runtime. Redis guarantees the script's atomic execution. Scripts can employ programmatic control structures and use most of the commands while executing to access the database.īecause scripts execute in the server, reading and writing data from scripts is very efficient. Redis lets users upload and execute Lua scripts on the server.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |