Whether it is a good idea or not is another story, but in practice, this happens a lot. Sometimes people store JSON objects in Redis. What did you mention about changing JSON? Note: you should use actual SHA1 hash returned by the SCRIPT LOAD command, the hash above is only an example. This is the unique hash of the script which you need to provide to the EVALSHA command to run the script: evalsha 4e6d8fc8bb01276962cce5371fa795a7763657ae 1 key:name No! Redis allows you to preload a script into memory with the SCRIPT LOAD command: script load “return redis.call(‘get’, KEYS)” Do I have to specify full script text to run it? Which means that we got values of the keys we wanted and in the correct order. You should see the following output: “value:3” Let’s run the following script: eval “local order = redis.call(‘zrange’, KEYS, 0, -1) return redis.call(‘hmget’,KEYS,unpack(order)) ” 2 order hkeys You might want to check hmset and zadd commands reference for details. These commands create a hash map at key hkeys and a sorted set at key order which contains selected keys from hkeys in a specific order. Let’s first set up our data by running these commands in redis-cli: hmset hkeys key:1 value:1 key:2 value:2 key:3 value:3 key:4 value:4 key:5 value:5 key:6 value:6 The order itself was specified by hash keys stored in a sorted set. I once used Lua scripts to return elements from a hash map in a particular order. Something more complex? A person planning to build a complex Redis script In our case, this process is redis-cli and you will see the result in your terminal window. 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. 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. No other Redis command can run while a script is executing.įor example, I use Lua scripts to change JSON strings stored in Redis. Also, all steps within a script are executed in an atomic way.Instead of doing these steps in the language of your application, you can do it inside Redis with Lua. Most tasks you do in Redis involve many steps. It might be helpful to check Redis commands reference while reading. You should have Redis installed on your system to follow this guide. You might’ve heard that it lets you run Lua scripts, but you are still not sure why. Redis is a popular in-memory grid used for interprocess communication and data storage.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |