Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to execute lua script in Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to execute the lua script in Redis. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Lua script debugging

(1) Environment preparation: local installation starts a redis service, assuming that the port number is 6379 and the password is pwd123

(2) according to (1), the command to enter the redis command line is:. / src/redis-cli-h 127.0.0.1-p 6379-a pwd123

(3) the command for debugging is:. / src/redis-cli-h 127.0.0.1-p 6379-a pwd123-- ldb-- eval. / tmp/aa01.lua key01 key2 key3, arg1

(4) in the above hit, there are three key and an arg parameter, which are directly separated by commas with spaces on both sides.

(5) after entering debug mode, you can use help, step, print commands to step through or print variables; / / print the passed key and parameters, print KEYS print ARGS

Note: about redis's lua script debug, I found a video on bilibili, which is official:

Redis lua debug function demonstration video: https://www.bilibili.com/video/av9437433/

2. Restrictions on the execution of lua scripts in Aliyun cluster version of redis

Https://help.aliyun.com/document_detail/92942.html

Lua usage restrictions

To ensure that all operations in the script are performed on the same slot, the cloud database Redis cluster version imposes the following restrictions on Lua scripts. If the limit is violated, an error may be reported as follows:

-ERR bad lua script for redis cluster, all the keys that the script uses should be passed using the KEYS arrayrn

-ERR eval/evalsha command keys must be in same slotrn

-ERR for redis cluster, eval/evalsha number of keys can't be negative or zerorn

3. How to deal with restrictions

(1) according to Aliyun's documentation, the key used for all commands in the script should be passed through the KEYS array; note: not even constants, must be passed using the KEYS array.

(2) to ensure that all key hash_tag are in the same slot, you can add {xx} before the key; for example, if there are two KEY:userId, orderId, you can use {slot} userId and {slot} orderId

(3) for all non-key parameters, please use ARGS to pass them, or constants (written directly in the script).

4. For instance

(1) suppose you use two key in your script: "userId" and "orderId".

(2) then the script is written in a similar way:

Redis.call ("incrby", KEYS [1], 1)

Redis.call ("incrby", KEYS [2], 1)

Note: the location of key in any redis.call command must be in the form of KEYS [n], that is, KEYS pass.

(3) how to write the program, java as an example:

String key1 = "{slot}" + "userId;String key2 =" {slot} "+" orderId "; byte [] [] keys = new byte [] [] {key1.getBytes (), key2.getBytes ()}; / / Mode 1Object result = redisTemplate.execute (luaScript, keys, args); / / Mode 2Object result = redisTemplate.execute (new RedisCallback () {@ Override public Object doInRedis (RedisConnection connection) throws DataAccessException {Boolean re = connection.eval (luaScript.getBytes (), ReturnType.BOOLEAN, 2, keys)) Return re;}})

This is how to execute the lua script in the Redis shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report