安装redis-cli
redis-cli连接远程服务 redis-cli.exe -h host -p port -a password
info server
查看redis服务器版本
C:\Users\ly-zhengzc>redis-cli.exe -h 172.26.123.154 -p 6379 -a 123456
172.26.240.154:6379> info server
# Server
redis_version:5.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:8a279a9d39031785
redis_mode:standalone
os:Linux 3.10.0-1160.24.1.el7.x86_64 x86_64
172.26.240.154:6379> set a 123
OK
172.26.240.154:6379> get a
"123"
172.26.240.154:6379> eval "return redis.call('get', 'a')" 0
"123"
172.26.240.154:6379>
执行lua脚本
执行lua脚本的两种方式
登录redis-cli后执行
C:\Users\ly-zhengzc>redis-cli.exe -h 172.26.123.154 -p 6379 -a 123456
172.26.240.154:6379> set a 123
OK
(0.62s)
172.26.240.154:6379> eval "return redis.call('get', 'a')" 0
"123"
172.26.240.154:6379> eval "return redis.call('set', 'a', '234')" 0
OK
172.26.240.154:6379> get a
"234"
172.26.240.154:6379> eval "return redis.call('get', 'a')" 0
"234"
172.26.240.154:6379>
redis-cli命令执行
redis-cli.exe -h host -p port -a auth eval lua_script key1 key2 , arg1 arg2 arg3
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 eval "return redis.call('set', 'a', '234')" 0
OK
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 eval "return redis.call('get', 'a')" 0
"234"
redis-cli命令执行,lua命令先保存在一个文件中,在执行
把lua脚本写到文件中,编辑的时候也方便一些redis-cli.exe -h host -p port -a auth --eval lua_file key1 key2 , arg1 arg2 arg3
get.lua
local key=KEYS[1]
return redis.call('get', key)
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 --eval get.lua a
"234"
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 --eval d:/tmp/lua/get.lua a
"234"
key参数和arg参数之间逗号,在逗号两边需要加空格,不然会报错
Lua redis() command arguments must be strings or integers
lua脚本 get
get.lua
local key=KEYS[1]
return redis.call('get', key)
执行
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 --eval get.lua a
"123"
D:\tmp\lua>
lua脚本 set
set.lua
local key=KEYS[1]
local value=ARGV[1]
return redis.call('set', key, value)
执行
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 --eval set.lua a , 234
OK
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 --eval get.lua a
"234"
D:\tmp\lua>
lua脚本 del
del.lua
local key=KEYS[1]
return redis.call('del', key)
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 --eval del.lua a
(integer) 1
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 --eval get.lua a
(nil)
lua脚本 del 匹配通配符删除
修改 del.lua文章来源:https://www.toymoban.com/news/detail-498762.html
local key=KEYS[1]
local licenseKeys = redis.call('keys', KEYS[1])
local del_count = 0
for i, v in ipairs(licenseKeys)
do
del_count = del_count +1
redis.call('del', v)
end
return del_count
第一次删除,删除了两个,第二次删除了0个文章来源地址https://www.toymoban.com/news/detail-498762.html
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 --eval del.lua a-*
(integer) 2
D:\tmp\lua>redis-cli.exe -h 172.26.240.154 -p 6379 -a 123456 --eval del.lua a-*
(integer) 0
到了这里,关于redis-cli操作lua的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!