In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
I. introduction
Redis is a high-performance key-value database, which greatly overcomes the shortcomings of key/value storage such as memcached. In some scenarios, it is a good supplement to the relational database. Thanks to ultra-high performance and rich data structures, Redis has become the preferred key-value storage system in the current architecture design.
Although more than 200 commands are provided on the Redis official website, it is impossible to avoid calling Redis many times in order to implement a small step of business logic.
Take the compare and set scenario as an example. If you use the Redis native command, you need to get the key from Redis, and then extract the value from it for comparison: if it is equal, no processing is done; if it is not equal or key does not exist, set key to the target value. Only a single point of compare and set operation needs to communicate with Redis twice.
In addition, this decentralized operation can not take advantage of the atomic characteristics of Redis and occupy multiple network IO.
Today we're going to talk about how to deal with the above scenarios gracefully.
II. Redis and Lua
Before we introduce Lua, we need to have a preliminary understanding of the language. Lua is a small scripting language that runs on almost all operating systems and platforms. We don't usually use Lua to handle particularly complex transactions, so we only need to understand some of the basic syntax of lua.
Since the advent of Redis, its developers are aware of the problems mentioned at the beginning, so Redis has supported Lua scripts since version 2.6. The new version of Redis also supports Lua Script debug. Interested partners can go to the Documentation on the official website to find the corresponding introduction and QuickStart.
With the Lua script, you can achieve significant improvements in the following areas when using Redis programs:
Reduce network overhead: the original N network requests can be completed with one request. The logic of the original N requests is completed on the Redis server, which reduces the network round-trip delay.
Atomic manipulation: Redis executes the entire script as a whole without being inserted by other commands. This is an important feature, so be sure to remember it in a small notebook. As to why it is an atomic operation, we will analyze it later.
Reuse: scripts sent by the client are permanently stored in Redis. This allows other clients to reuse the script without using code to complete the same logic.
So now there is a saying: if you want to learn Redis well, you must Lua Script.
Third, realize compare and set through Lua script
Next we will implement a simple compare and set and use this example to feel the new experience that Lua scripts bring to Redis usage.
First take a look at how to get Redis to execute the Lua script.
3.1 EVALRedis 127.0.0.1 EVAL script numkeys key for Redis [key...] Arg [arg...]
Script: the argument is a Lua 5. 1 script. The script does not have to (and should not) be defined as a Lua function.
Numkeys: used to specify the number of key name parameters.
Key [key...]: starting with the third parameter of EVAL, it represents the key used in the script. In Lua, these key name parameters can be accessed in the form of 1 as the base address through the global variable KEYS array (KEYS [1], KEYS [2], and so on).
Arg [arg...]: additional parameters, accessed in Lua through an array of global variables ARGV, in a form similar to KEYS variables (ARGV [1], ARGV [2], and so on).
Here is an example from the official website.
The above script returns the input parameter directly.
Eval is the Redis keyword
The content in the first quote is the Lua script
2 is the number of parameters
Key1 and key2 are the input parameters of KEYS [1] and KEYS [2].
First and second are the input parameters of ARGV [1] and ARGV [2].
You can simply think of KEYS [1], KEYS [2], ARGV [1], ARGV [2] as placeholders. Jiaozuo traditional Chinese Medicine Gastrointestinal Hospital: http://jz.lieju.com/zhuankeyiyuan/37756433.htm
3.2 execute script files and cache scripts
If you can only write and execute scripts from the command line, won't you freak out when you encounter complex scripts?
Let's take a look at how to get Redis to execute Lua script files while also verifying the reuse nature of lua scripts (we no longer need to delete certain key in batches that conform to specific rules on a regular basis).
Redis 127.0.0.1 SCRIPT LOAD scriptRedis 6379 > EVALSHA sha1 numkeys key [key...] Arg [arg...]
Redis provides a SCRIPTLOAD command, followed by a script that is the Lua script. Command to add the script script to the script cache, but does not execute the script immediately. After the command is executed, Redis returns a SHA1 string, and the second EVALSHA command can be executed.
It is important to note that scripts can remain in the cache for an infinite amount of time until the SCRIPT FLUSH is executed. Let's take a look at the effect.
Redis also supports direct execution of Lua script files. First write and store a Lua script.
Then call the Redis-cli-eval command
The syntax of the Redis-cli-eval command is basically the same as the original eval syntax.
3.3.Using Lua scripts to implement compare and set
The implementation logic of compareand set is as follows: first get the value of the specified key in Redis, and then compare it with the given value: if equal, set key to the target value and return an identifier; if not, do nothing and return an identifier. Zhengzhou infertility hospital ranking: http://www.zzfkyy120.com/
If Redis.call ('get', KEYS [1]) = ARGV [1] then Redis.call (' set', KEYS [1], ARGV [2]); return 1else return 0 end
Let's test the script.
First write a value value to the specified key compareAndSet:key of Redis
Execute lua script in Redis
You can see that the first execution returns 1, indicating that the modification is successful; and when the original parameter is used, 0 is returned, indicating that no changes have been made. Let's check the key of compareAndSet:key again.
You can see that the key of compareAndSet:key has been changed to new_value.
IV. Summary
We implemented a simple compareAndSet operation through a lua script.
Let's use this example to verify the features mentioned at the beginning.
Reduce network overhead: without using scripts, we need to interact with Redis at least twice to implement a compareAndSet, but now we only need to perform one operation
Atomic manipulation: thanks to the design of Redis, Redis executes the entire script as a whole without being inserted by other commands. Therefore, in the process of writing scripts, there is no need to worry about race conditions, there is no need to use transactions, interested can be Baidu or wait for subsequent articles to be updated
Reuse: a series of operations can be encapsulated into a Lua script, stored in a file or Redis, and can be called directly the next time you use it.
Reading here, I hope you have some understanding of Redis+Lua, and can use scripts to complete some simple composite operations. In the future, we will continue to update some distributed data structures based on Lua scripts and java programs, such as delay queues, reentrant locks, etc., which can be followed by interested partners.
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.