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 > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the Go language how to operate Redis through Lua script related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you read this Go language how to operate Redis through Lua script article will have a harvest, let's take a look at it.
Preface
To reduce the cost of communicating with Redis in one of my basic libraries, I encapsulated a series of operations in LUA scripts to simplify operations with the help of EVAL commands provided by Redis.
Features that EVAL can provide:
Several operations can be encapsulated in a LUA script. If there are multiple Redis instructions, you only need to send all the parameters to Redis at once to get the result.
Redis ensures that no other commands will be inserted and executed during the Lua script, providing atomicity like database transactions.
Redis caches the script according to the SHA value of the script, and the cached script does not need to transfer Lua code again, reducing the communication cost. In addition, if you change the Lua script in your own code, Redis will certainly use the latest code when it is executed.
By importing common Go libraries such as "github.com/go-redis/redis", you can implement the following code.
Generate a Lua script
/ / KEYS: key for record// ARGV: fieldName, currentUnixTimestamp, recordTTL// Update expire field of record key to current timestamp, and renew key expirationvar updateRecordExpireScript = redis.NewScript (`redis.call ("EXPIRE", KEYS [1], ARGV [3]) redis.call ("HSET", KEYS [1], ARGV [1], ARGV [2]) return 1`)
When this variable is created, the Lua code is not executed and an existing Redis connection is not required.
Lua script support provided by Redis. There are two arrays KEYS and ARGV by default. KEYS represents several keys passed in when the script is running, and ARGV represents several passed parameters. Since the Lua code needs to be concise and difficult to read, it is best to write some comments for these parameters
Note: the above code uses ``across lines. Although the line in which `is located is blank, it will be considered to be a line. Do not misread the code line number when you report an error.
Run a Lua script
UpdateRecordExpireScript.Run (c.Client, [] string {recordKey (key)}, expireField, time.Now (). UTC (). UnixNano (), int64 (c.opt.RecordTTL/time.Second)) .Err ()
At run time, Run will first try to run the script through the cache through EVALSHA. If there is no cache, it will be run using EVAL, and the entire Lua script will be passed into Redis.
Limitations of Lua scripts
Redis does not provide additional packages to introduce, such as os, etc., only redis is available.
The Lua script will run in a function, and all variables must be declared using local
When return returns more than one value, Redis will only give you the first one
Type restrictions in script
When the script returns nil, what you get in Go is err = redis.Nil (same as Get cannot find the value)
When the script returns false, what you get in Go is nil, and when the script returns true, you get 1 in Go of type int64.
When the script returns {"ok":...}, what you get in Go is the status type (true/false) of redis
When the script returns {"err":...}, the error value is obtained in Go, which can also be achieved through return redis.error_reply ("My Error").
When the script returns the number type, the Go gets the int64 type
All values in the KEYS/ARGV of the incoming script are of type string. To convert to a numeric type, you should use to_number.
What happens if the script runs for a long time?
While the Lua script is running, in order to avoid contaminating the data by other operations, no other commands can be executed during this period, and other requests can not be executed until the execution is complete. When the execution time of the Lua script exceeds the lua-time-limit, other requests will receive Busy errors unless they are SCRIPT KILL (kill the script) or SHUTDOWN NOSAVE (close Redis without saving the result)
For more information, please refer to the address below. I am here to provide some summaries based on my experience with Go. Https://redis.io/commands/eval
A more "complex" script that requires that when getting a key value, if the value is accessed more often, the lifetime is extended. In addition, the update time should be compared. If no update is needed, the obtained value will be returned directly, otherwise redis.Nil will be returned.
/ / KEYS: rec:key, key// ARGV: currentUnixTimestamp, hotHit, recordTTL, ttl// When there's a hit,var fetchRecordScript = redis.NewScript (local value = redis.call ("GET", KEYS [2]) if (value = = nil) then return nil end local hit = redis.call ("HINCRBY", KEYS [1], "hit", 1) redis.call ("EXPIRE", KEYS [1]) ARGV [3]) local minHotHit = tonumber (ARGV [2]) local keyTTL = tonumber (ARGV [4]) if (hit > minHotHit) then keyTTL = keyTTL * 2 end redis.call ("EXPIRE", KEYS [2], keyTTL) local expire = tonumber (redis.call ("HGET", KEYS [1], "expire") local unixTime = tonumber (ARGV [1]) if (expire = nil or expire < unixTime) then return nil else return value end) / KEYS: key for record// ARGV: fieldName, currentUnixTimestamp RecordTTL// Update expire field of record key to current timestamp, and renew key expirationvar updateRecordExpireScript = redis.NewScript (redis.call ("EXPIRE", KEYS [1], ARGV [3]) redis.call ("HSET", KEYS [1], ARGV [1], ARGV [2]) return 1) this is the end of the article on "how to operate Redis through Lua scripts in Go language" Thank you for reading! I believe that everyone has a certain understanding of "how to operate Redis through Lua scripts in Go language". If you want to learn more, you are welcome to follow the industry information channel.
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.