How can Redis use the lua script instance tutorial

  • 2020-06-12 10:55:27
  • OfStack

preface

The redis website is full of more than 200 commands, which may seem like a lot, but they're all pre-defined and you can't customize them to your own purpose,

So does it feel like you still have a feeling of being bound? That's the right feeling...

As it happens, the big boss of redis has given you the solution to this problem, the Lua script, and the latest version of redis supports Lua Script debug, which should also be the next version of Redis 1

If you want to learn Redis well, you must know Lua Script...

Without further ado, let's take a look at the details

Version: available from 2.6.0.

Time complexity: Depends on the script being executed.

Benefits of using Lua scripts:

Reduce network overhead. Multiple requests can be sent once in the form of a script to reduce network latency. Atomic manipulation. redis executes the entire script as a unit and is not inserted by other commands. So you don't have to worry about race conditions during scripting and you don't have to use transactions. Reuse. The steps sent by the client are perpetuated in redis so that other clients can reuse the 1 script without having to use the code to complete the same logic.

How to use

The basic use

Command format:

[

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

]

Description:

script is the first parameter and is the Lua 5.1 script. The script does not need to define the Lua function (nor should it). The second parameter, numkeys, specifies how many key follow. key [key...]. Is the key to be operated on, and can be specified in multiple keys, obtained from KEYS[1], KEYS[2] in the lua script arg [arg...]. , parameters obtained in lua script by ARGV[1], ARGV[2].

Simple examples:


127.0.0.1:6379> eval "return ARGV[1]" 0 100 
"100"
127.0.0.1:6379> eval "return {ARGV[1],ARGV[2]}" 0 100 101
1) "100"
2) "101"
127.0.0.1:6379> eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second
1) "key1"
2) "key2"
3) "first"
4) "second"

127.0.0.1:6379> eval "redis.call('SET', KEYS[1], ARGV[1]);redis.call('EXPIRE', KEYS[1], ARGV[2]); return 1;" 1 test 10 60
(integer) 1
127.0.0.1:6379> ttl test
(integer) 59
127.0.0.1:6379> get test
"10"

Note:

{} in lua is the index type table, similar to an array. redis. call() can call the redis command.

From the command line

If you use the ES78en-ES79en command directly, the format is a bit different:


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

Points to note:

eval is followed by the lua script file with the.lua suffix Instead of writing numkeys, use separate. Notice the Spaces before and after.

Example:

incrbymul.lua


local num = redis.call('GET', KEYS[1]); 

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

Command line run:


$ 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 the atomicity of a number by N, we implemented the Lua script to ensure that it would not be interrupted by other clients.

Do you use in the phpredis

Follow the above example:

incrbymul.php


<?php 

$lua = <<<EOF
local num = redis.call('GET', KEYS[1]); 

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

EOF;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$ret = $redis->eval($lua, array("lua:incrbymul", 2), 1);
echo $ret;

Run:


$ redis-cli set lua:incrbymul 0
OK
$ redis-cli incr lua:incrbymul
(integer) 1
$ php incrbymul.php 
2
$ php incrbymul.php 
4

eval prototype:


Redis::eval(string script, [array keys, long num_keys])

The third argument to the eval function is the number of KEYS, according to which phpredis distinguishes KEYS from ARGV.

reference

1. Use lua script in redis to increase your flexibility 5 pretend bility-1 line code farmers

https://www.ofstack.com/article/148830.htm

2. Redis executes the sample Lua script - yanghuahui

https://www.ofstack.com/article/148833.htm

3. EVAL-Redis

https://redis.io/commands/eval

phpredis example of LUA script - jingtan column

https://www.ofstack.com/article/148838.htm

5, lua - book

http://me.52fhy.com/lua-book/

conclusion


Related articles: