关于redis:redis如何执行lua脚本附lua部分基本语法

3次阅读

共计 2620 个字符,预计需要花费 7 分钟才能阅读完成。

在客户端命令行执行 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 = 0
for i = 1, 100
do
        sum = sum + i
end
-- 输入后果为 5050
print(sum)

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

for i = 1, #myArray
do
        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 = 0
local int i = 0
while i <= 100
do
    sum = sum +i
    i = i + 1
end
-- 输入后果为 5050
print(sum)

if else

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

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

哈希

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 .. str2
end
--"hello world"
print(contact("hello", "world"))

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

正文完
 0