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 use lua script in redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to use lua scripts in redis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

One: Lua script

Coincidentally, the big boss of redis gave you a way to solve this problem, that is, Lua script, and the latest version of redis also supports Lua Script debug, which should also be one of the future Redis.

A development trend, if you want to learn Redis well, you must Lua Script.

Interestingly, a video tutorial is also available on the official website to teach you how to operate Debug. [https://redis.io/topics/ldb] if the video on youtube is hit by a wall, remember

Go to VPN. I don't know if this kind of ceiling lamp is sold on Taobao.

Second: using Redis-Cli Lua Script to solve several flexibility problems

1. Problems with Lua syntax

Lua is a programming language, so this is beyond the scope of redis itself. If you want to study hard, you can take a look at http://www.lua.org/ 's official website and download it for fun.

For example, here I downloaded a windows version of the lua compiler, the specific syntax will not be detailed. With this theme, let's move on to the next step.

2. The use of Eval

EVAL script numkeys key [key...] Arg [arg...]

First of all, you must know the syntax format of eval, where:

Script: your lua script

Numkeys: number of key

Key: alternative symbols for various data structures in redis

Arg: your custom parameters

Ok, the template may not be very clear at first glance. I can demonstrate it with a small example on the official website below:

Eval "return {KEYS [1], KEYS [2], ARGV [1], ARGV [2]}" 2 username age jack 20

What does the above string of code probably mean? The string for the first parameter is script, which is the lua script. 2 indicates the number of keys, KEYS [1] is the placeholder of username, and KEYS [2] is

The placeholder of age, ARGV [1] is the placeholder of jack, ARGV [2] is the placeholder of 20, and so on, so the end result should be: {return username age jack 20} no.

It's a bit like the placeholder in C#: {0}? Let me show you in Redis:

[root@localhost Desktop] # redis-cli127.0.0.1:6379 > eval "return {KEYS [1], KEYS [2], ARGV [1], ARGV [2]}" 2 username age jack 201) "username" 2) "age" 3) "jack" 4) "20" 127.0.0.16379 >

Under normal circumstances, we do not write lua scripts directly in redis-cli, which is very difficult to edit. Usually, we put lua script into a lua file and execute the lua script, such as

It goes like this:

Then we execute it with the following command, which is different from the one described earlier, with the parameters-- eval script key1 key2, arg1 age2 mode, key and value separated by a comma.

Finally, we saw that the data came out, right?

[root@localhost Desktop] # redis-cli-- eval / usr/redis/sbin/1.lua username age, jack 201) "username" 2) "age" 3) "jack" 4) "20" [root@localhost Desktop] #

Three: actual combat

Below, I can conceive of a few small cases to be solved through lua.

1. Get all the data in the List of the specified key through the lua script

Local key=KEYS [1] local list=redis.call ("lrange", key,0,-1); return list

The redis.call is used to execute the lrange command of list in redis. Next, I insert three pieces of data into person through lpush, as follows:

[root@localhost Desktop] # redis-cli127.0.0.1:6379 > lpush person mary jack peter (integer) 3127.0.0.1 lpush person mary jack peter 6379 >

Then let's execute this lua script, the effect is shown below, isn't it a great feeling?

With this 1: 1 effect, you can play some more complex operations. For example:

two。 According to the IDList passed from the outside, do the lua script logic of "collecting and deduplicating":

Local key=KEYS [1]; local args=ARGVlocal iTuno: local result= {}; for m in ipairs (args) do local ishit=redis.call ("sismember", key,n); if (ishit) then table.insert (result,1,n); end endreturn result

two。 Find all the data in hash whose age is less than the specified value. The lua script is as follows:

Local result= {}; local myperson=KEYS [1]; local nums=ARGV [1]; local myresult = redis.call ("hkeys", myperson); for I in ipairs (myresult) do local hval= redis.call ("hget", myperson,v); redis.log (redis.LOG_WARNING,hval); if (tonumber (hval))

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

Database

Wechat

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

12
Report