![lua table insert key lua table insert key](https://www.netio-products.com/files/styles/application_note_image/public/AN08_creating-rule-through-web-interface-in-Lua-scripting-language_1.png)
Our second script does a little more than just running a single command - it also returns a value: eval “return redis.call(‘get’, KEYS)” 1 key:nameĮverything returned by the script is sent to the calling process. According to the documentation: Redis uses the same Lua interpreter to run all the commands In the case of the set command, these arguments are key and value. The first argument is the name of this command followed by its parameters. With redis.call you can execute any Redis command. Our first script consists of a single statement: the redis.call function: redis.call(‘set’, KEYS, ARGV) Can you explain the script? Doge after seeing the script above The output should be ”value”, which means that the previous script successfully set the key “key:name”. We are going to do this by running another script which gets the key from Redis:Įval “return redis.call(‘get’, KEYS)” 1 key:name Let’s now check if the script completed successfully. So you shouldn’t specify KEYS as 0 and then provide all keys within the ARGV table. It is considered a good practice to provide keys which the script uses as KEYS, and all other arguments as ARGV. As you already guessed, the above command sets the key key:name to value value. In this example, we provide a single ARGV-argument: string value. We can provide any number of arguments after the keys, which will be available in Lua as the ARGV table. Note, that Lua indexed tables start with index 1, not 0. In our case, it contains a single value key:name at index 1. They are accessible as KEYS table within the script. Immediately after this number, we need to provide these keys, one after another. We specify how many keys the script requires with the number immediately following it. Script arguments fall into two groups: KEYS and ARGV. The ”redis.call(‘set’, KEYS, ARGV)”string is our script which is functionally identical to the Redis’s set command.
![lua table insert key lua table insert key](https://images.sampletemplates.com/wp-content/uploads/2015/02/Word-Table-of-Contents-Template.jpg)
The EVAL command is what tells Redis to run the script which follows. Now run the following command: eval “redis.call(‘set’, KEYS, ARGV)” 1 key:name value Let’s start by running scripts via redis-cli. Also, I am providing working examples in this article. If you know any language of the C family, you should be okay with Lua. But I don’t know any Lua A person who doesn't know any luaĭon’t worry, Lua is not very hard to understand. I describe this in detail closer to the end of this article.
![lua table insert key lua table insert key](https://i.pinimg.com/474x/e9/72/72/e97272d0a7430b11ababa278be9d6d69.jpg)
No other Redis command can run while a script is executing.įor example, I use Lua scripts to change JSON strings stored in Redis.