# 调用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"
# 指定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
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
<?php $redis = new Redis(); $redis->connect('192.168.1.201', 6379); $redis->auth('123456'); $script = <<<EOF return redis.call('lpush', KEYS[1], ARGV[1]) EOF; $key1 = 'goods_list'; $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);
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!