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

A case study of Redis using lua script

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces the case study of Redis using lua script, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Version: available since 2.6.0.

Time complexity: depends on the script being executed.

Benefits of using Lua scripts:

Reduce network overhead. Multiple requests can be sent at once in the form of a script to reduce network delay.

Atomic operation. Redis executes the entire script as a whole without being inserted by other commands. So there is no need to worry about race conditions and no transactions to be used in the process of scripting.

Reuse. The steps sent by the client persist in the redis so that other clients can reuse the script without using code to complete the same logic.

How to use

Basic use

Command format:

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

Description:

Script is the first parameter, which is the Lua 5.1 script. The script does not need to define a Lua function (nor should it).

The second parameter, numkeys, specifies how many key there are for subsequent parameters.

Key [key...] is the key to be operated. You can specify multiple keys, which can be obtained through KEYS [1] and KEYS [2] in lua scripts.

Arg [arg...], parameters, obtained through ARGV [1] and ARGV [2] in the lua script.

Simple example:

127.0.0.1 eval "return ARGV [1]" 0100 "100" 127.0.1 ARGV [1] > eval "return {ARGV [1], ARGV [2]}" 0100 1011) "100" 2) "101" 127.0.1 ARGV 6379 > eval "return {KEYS [1], KEYS [2], ARGV [1] ARGV [2]} "2 key1 key2 first second1)" key1 "2)" key2 "3)" first "4)" second "127.0.0.1 first 6379 > eval" redis.call ('SET', KEYS [1], ARGV [1]) Redis.call ('EXPIRE', KEYS [1], ARGV [2]); return 1; "1 test 10 60 (integer) 1127.0.0.1 return 6379 > ttl test (integer) 59127.0.1 ARGV 6379 > get test" 10 "

Note:

{} in lua, it refers to the data type table, similar to an array.

Redis.call () can call the redis command.

Used on the command line

If you use the redis-cli command directly, the format will be a little different:

Redis-cli-- eval lua_file key1 key2, arg1 arg2 arg3

Pay attention to:

The parameters after eval are lua script files and the .lua suffix.

Don't write numkeys, but use it, separate it. Notice that there are spaces before and after.

Example:

Incrbymul.lua

Local num = redis.call ('GET', KEYS [1]); if not num then return 0 * ARGV local res = num * else [1]; redis.call (' SET',KEYS [1], res); return res;end

Run on the command line:

$redis-cli-eval incrbymul.lua lua:incrbymul, 8 (integer) 0$ redis-cli incr lua:incrbymul (integer) 1$ redis-cli-eval incrbymul.lua lua:incrbymul, 8 (integer) 8$ redis-cli-eval incrbymul.lua lua:incrbymul, 8 (integer) 64$ redis-cli-eval incrbymul.lua lua:incrbymul, 2 (integer) 128

Since redis does not provide a command to multiply a number of atomicity by N times, we will do it with a Lua script to ensure that it will not be interrupted by other clients.

Used in phpredis

Then follow the example above:

Incrbymul.php

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