在客户端命令行执行Lua脚本

$ cat /tmp/script.lua
return redis.call('SET',KEYS[1],ARGV[1])
$ redis-cli --eval /tmp/script.lua location:hastings:temp , 23
OK
note:命令行中应用逗号分隔KEYS和ARGV,KEYS和ARGV如果存在多个,两头应用一个和多个空格分隔。留神:逗号前后,必须含有至多一个空格。
场景一,前后都有空格:
redis-cli --eval /tmp/script.lua location:hastings:temp , 23
KEYS={"location:hastings:temp"} ARGV={"23"}

场景二,前面有空格
redis-cli --eval /tmp/script.lua location:hastings:temp, 23
KEYS={"location:hastings:temp,","23"} ARGV={}

场景三,后面有空格
redis-cli --eval /tmp/script.lua location:hastings:temp ,23
KEYS={"location:hastings:temp"; ",23"} ARGV={}

应用redis命令执行lua脚本

eval

eval script(脚本内容) numkeys(key个数) key [key ...](key列表) arg [arg ...](参数列表)

eval 'return "hello " .. KEYS[1] .. ARGV[1]' 1 redis world
"hello redisworld"

evalsha

加载脚本:script load命令能够将脚本内容加载到Redis内存中,例如下
面将test.lua加载到Redis中,失去SHA1
$redis-cli script load "$(cat test.lua)"
"c40bee28ef52798c0a894fc857070c93bd107c40"

script exists sha1 [sha1 ...] 用于判断sha1是否曾经加载到Redis内存中

script exists c40bee28ef52798c0a894fc857070c93bd107c40 aa
1) (integer) 1
2) (integer) 0

script flush
于革除Redis内存曾经加载的所有Lua脚本

script flush
OK
script exists c40bee28ef52798c0a894fc857070c93bd107c40 aa
1) (integer) 0
2) (integer) 0

script kill
用于杀掉正在执行的Lua脚本

eval 'while 1==1 do end' 0

在另一个客户端执行 script kill时,显示如下音讯
(error) ERR Error running script (call to f_c045d3ae3b3eca855e00c772db40aa560b3a1fc8): @user_script:1: Script killed by user with SCRIPT KILL...
(9.41s)

get hello
(error) BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.
(1.21s)
script kill
OK

evalsha 脚本 SHA1 值 key 个数 key 列表 参数列表
evalsha c40bee28ef52798c0a894fc857070c93bd107c40 1 red wor
"hello redwo"

Lua语言简介

数据类型:booleans布尔、numbers数字、strings字符串、tables表格。
"--"是Lua语言的正文
local代表val是一个局部变量,如果没有local代表是全局变量
print函数能够打印出变量的值

字符串:

-- 定义一个字符串
local strings val = "world"
-- 后果是 "world"
print(val)

数组

Lua的数组下标从1开始计算
local tables myArray = {"redis", "jedis", true, 88.0}
--true
print(myArray[3])

for循环

上面代码会计算1到100的和,关键字for以end作为结束符

local int sum = 0for i = 1, 100do        sum = sum + iend-- 输入后果为 5050print(sum)

要遍历myArray,首先须要晓得tables的长度,只须要在变量前加一个#
号即可

for i = 1, #myArraydo        print(myArray[i])end

Lua还提供了内置函数ipairs,应用for index,value
ipairs(tables)能够遍历出所有的索引下标和值

for index,value in ipairs(myArray)do        print(index)        print(value)end

while循环

上面代码同样会计算1到100的和,只不过应用的是while循环,while循
环同样以end作为结束符

local int sum = 0local int i = 0while i <= 100do    sum = sum +i    i = i + 1end-- 输入后果为 5050print(sum)

if else

要确定数组中是否蕴含了jedis,有则打印true,留神if以end结尾,if后
紧跟then

local tables myArray = {"redis", "jedis", true, 88.0}for i = 1, #myArraydo    if myArray[i] == "jedis"    then        print("true")        break    else        --do nothing    endend

哈希

local tables user_1 = {age = 28, name = "tome"}
--user_1 age is 28
print("user_1 age is " .. user_1["age"])

for key,value in pairs(user_1)     do print(key .. value)end

函数

--contact 函数将两个字符串拼接:function contact(str1, str2)    return str1 .. str2end--"hello world"print(contact("hello ", "world"))

@局部文段摘自于付磊的Redis开发与运维