Home > Database > Redis > Example analysis of redis script command execution problems

Example analysis of redis script command execution problems

WBOY
Release: 2023-05-26 15:28:06
forward
1511 people have browsed it

1. Execute in the redis-cli command line:

# 调用redis命令设置缓存
# 不传参数
eval "return redis.call('set', 'name1', 'Tom')" 0
# 传入1个值参数
eval "return redis.call('set', 'name2', ARGV[1])" 0 "Tom"
# 传入1个键名参数和1个值参数
eval "return redis.call('set', KEYS[1], ARGV[1])" 1 "name3" "Tom"
Copy after login

2. Execute in the linux command line:

# 指定lua脚本路径,后面可跟上参数,与redis-cli中不同,此处不需要指定KEYS的数量,但是需要用英文逗号隔开KEYS和ARGV参数,逗号前后至少保留1个空格,否则报错
redis-cli -a password --eval ./sadd_script.lua subjects , math
redis-cli -a password --eval ./sadd_script.lua subjects , history
Copy after login

Example analysis of redis script command execution problems

Define your own needs in the lua script Business code, demo:

local setName = KEYS[1]
local setValue = ARGV[1]
if string.len(setName) > 0 and string.len(setValue) > 0
then
    return redis.call('sadd', setName, setValue)
else
    return 0
end
Copy after login

3, executed in php code:

<?php
$redis = new Redis();
$redis->connect(&#39;192.168.1.201&#39;, 6379);
$redis->auth(&#39;123456&#39;);
$script = <<<EOF
    return redis.call(&#39;lpush&#39;, KEYS[1], ARGV[1])
EOF;
$key1 = &#39;goods_list&#39;;
$value1 = mt_rand(10000, 99999);
//eval($script, $args = array(), $numKeys = 0),numKeys声明args参数中KEYS数量,剩下的则都为ARGV参数
$res = $redis->eval($script, [$key1, $value1], 1);
var_dump($res);
Copy after login

The above is the detailed content of Example analysis of redis script command execution problems. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template