安装redis-cli
redis-cli.exe redis-cli -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>
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.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 123456eval "return redis.call('get', 'a')" 0
"234"
把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
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>
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>
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)
修改 del.lua
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个
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