In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to execute the Lua script in Redis. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Preface
Redis has introduced support for Lua scripts since version 2.6. by embedding the Lua environment in the server, Redis clients can use Lua scripts to execute multiple Redis commands directly on the server.
Where you can evaluate the entered script directly using the EVAL command:
Redis > EVAL "return 'hello world'" 0 "hello world"
The benefits of using scripts are as follows:
1. Reduce network overhead: the operation of the original 5 network requests can be completed with one request, and the logic of the previous 5 requests is completed on the redis server. The use of scripts reduces the network round-trip delay.
two。 Atomic manipulation: Redis executes the entire script as a whole without being inserted by other commands.
3. Reuse: scripts sent by clients are permanently stored in Redis, meaning that other clients can reuse the script without using code to complete the same logic.
Redis executes Lua script
To achieve an access frequency control, an ip frequently visits the page in a short period of time, which needs to be recorded and detected, and can be efficiently implemented through Lua scripts.
On the redis client machine, create a new file ratelimiting.lua with the following contents
Local times = redis.call ('incr',KEYS [1]) if times = = 1 then redis.call (' expire',KEYS [1], ARGV [1]) endif times > tonumber (ARGV [2]) then return 0endreturn 1
How do you test this script on the redis client machine? As follows:
Redis-cli-eval ratelimiting.lua rate.limitingl:127.0.0.1, 10 3
The eval parameter tells redis-cli to read and run the following Lua script, where ratelimiting.lua is the location of the script, followed by parameters passed to the Lua script. The rate.limiting:127.0.0.1 before "," is the key to be operated, which can be obtained with KEYS [1] in the script, and the following 10 and 3 are parameters, which can be obtained using ARGV [1] and ARGV [2] in the script. Note: the spaces on both sides cannot be omitted, otherwise there will be an error.
Combined with the contents of the script, we can see that the function of this command is to limit the access frequency to a maximum of 3 times every 10 seconds, so running this command in the terminal will find that 1 is returned when the access frequency is less than or equal to 3 times in 10 seconds, otherwise 0 is returned.
The test runs are as follows:
[root@rhel6 redis-learning] # redis-cli-eval ratelimiting.lua rate.limitingl:127.0.0.1, 10 3 (integer) 1 [root@rhel6 redis-learning] # redis-cli-eval ratelimiting.lua rate.limitingl:127.0.0.1, 10 3 (integer) 1 [root@rhel6 redis-learning] # redis-cli-eval ratelimiting.lua rate.limitingl:127.0.0.1 10 3 (integer) 1 [root@rhel6 redis-learning] # redis-cli-eval ratelimiting.lua rate.limitingl:127.0.0.1, 10 3 (integer) 0 [root@rhel6 redis-learning] # redis-cli-eval ratelimiting.lua rate.limitingl:127.0.0.1, 10 3 (integer) 0 read the above Do you know more about how to execute Lua scripts in Redis? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.