In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to solve the problems encountered in the use of Redis and Lua, the article is very detailed, has a certain reference value, interested friends must read it!
problem
When executing key or field in Redis where get or hget does not exist, the return value is explicit at the terminal (nil), similar to the following
127.0.0.1 6379 > get test_version (nil)
If you judge whether the obtained value is null or not in the Lua script, you will be confused. You can just use nil to judge the null value, but this is not the case in the goose script, as shown below:
127.0.0.1 get test_version (nil) 127.0.0.1 get test_version 6379 > EVAL "local a = redis.call ('get',KEYS [1]) print (a) if a = =' nil' then return 1 else return 0 end" 1 test_version test_version (integer) 0
Let's take a look at the data type of the result returned by executing the Lua script
127.0.0.1 get test_version (nil) 127.0.0.1 get test_version 6379 > EVAL "local a = redis.call ('get',KEYS [1]) return type (a)" 1 test_version test_version "boolean"
As you can see from the above script, when the result returned by Redis is (nil), its real data type is boolean, so we directly judge that nil is problematic.
Redis official documentation
By flipping through the official documents, find the passage shown below
Redis to Lua conversion table.
Redis integer reply-> Lua number
Redis bulk reply-> Lua string
Redis multi bulk reply-> Lua table (may have other Redis data types nested)
Redis status reply-> Lua table with a single ok field containing the status
Redis error reply-> Lua table with a single err field containing the error
Redis Nil bulk reply and Nil multi bulk reply-> Lua false boolean type
Lua to Redis conversion table.
Lua number-> Redis integer reply (the number is converted into an integer)
Lua string-> Redis bulk reply
Lua table (array)-> Redis multi bulk reply (truncated to the first nil inside the Lua array if any)
Lua table with a single ok field-> Redis status reply
Lua table with a single err field-> Redis error reply
Lua boolean false-> Redis Nil bulk reply.
Solution
Through the official documents, we know that judging that the Lua script returns a null value is used, and the true/false should be judged directly. The modified judgment script is as follows
127.0.0.1 get test_version (nil) 127.0.0.1 local 6379 > EVAL "local a = redis.call ('get',KEYS [1]) if a = false then return' empty' else return 'not empty' end" 1 test_version test_version "empty" are all the contents of this article entitled "how to solve the problems encountered in the use of Redis and Lua". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.