Skip to content

Commit 396c85d

Browse files
committedJun 19, 2019
lua
1 parent 9b0b923 commit 396c85d

File tree

1 file changed

+73
-22
lines changed

1 file changed

+73
-22
lines changed
 

‎Storage/Redis/project/lua/script.lua

+73-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
21
-- ******************************************** lua 基本语法 ********************************************
32

43
-- 变量声明
5-
local key = {KEYS[1],KEYS[2]};
6-
local value = {ARGV[1],ARGV[2]};
4+
local retTable = {};
5+
6+
-- 取参数
7+
local k1 = KEYS[1];
8+
local k2 = KEYS[2];
9+
local v1 = ARGV[1];
10+
local v2 = ARGV[2];
11+
table.insert(retTable, {k1, k2, v1, v2}); -- ["key1","key2","1","2.3"]
712

8-
local table = {'this', 'is', 'table'};
13+
-- 变量声明
14+
local tableIns = {'this', 'is', 'table'};
915
local luaNil = nil;
1016
local boolean = true
1117
local number = 3.2;
1218
local str = 'this is string';
13-
14-
19+
-- 注意 nil 的位置
20+
table.insert(retTable, {tableIns, boolean, number, str, luaNil}); -- [["this","is","table"],1,3,"this is string"]
1521

1622
-- 类型判断
1723
local tk1 = type(KEYS[1]);
1824
local tk2 = type(KEYS[2]);
1925
local tv1 = type(ARGV[1]);
2026
local tv2 = type(ARGV[2]);
27+
table.insert(retTable, {tk1, tk2, tv1, tv2}); -- ["string","string","string","string"]
28+
29+
-- 类型转换
30+
local numV1 = tonumber(v1);
31+
local numV2 = tonumber(v2);
32+
local str1 = tostring(1.1);
33+
local str2 = 'this'..' is'..' str' -- 字符串拼接
34+
local str2Len = #str2;
35+
36+
table.insert(retTable, {numV1, type(numV1), numV2, type(numV2), str1, type(str1), str2, type(str2), str2Len, type(str2Len)});
37+
-- [1,"number",2,"number","1.1","string","this is str","string",11,"number"]
2138

2239
-- 函数定义
2340
local function factorial(num)
@@ -27,30 +44,64 @@ local function factorial(num)
2744
return num * factorial(num-1);
2845
end
2946
end
30-
31-
32-
47+
table.insert(retTable, factorial(3)); -- 6
48+
49+
-- 多返回值, 返回最大值和其索引
50+
local function max(a) -- 不确定参数个数, 可用 ... 表示变长参数, 类似 js 中的 ...rest
51+
local index = 1;
52+
local m = a[index];
53+
54+
for i, val in ipairs(a) do
55+
if val > m then
56+
index = i;
57+
m = val;
58+
end
59+
end
60+
return m, index
61+
end
62+
local m, index = max({1, 9, 5, 3,7});
63+
table.insert(retTable, {m, index}); -- [9,2]
3364

3465
-- ******************************************** 与redis 交互 ********************************************
66+
local redisTable = {};
67+
table.insert(retTable, redisTable);
3568
-- redis.call(), 执行命令出错, 将导致脚本退出
3669
redis.call('set', 'lua-key1', 'lua-value1');
37-
local v1 = redis.call('get', 'lua-key1');
70+
local rv1 = redis.call('get', 'lua-key1');
71+
table.insert(redisTable, rv1); -- "lua-value1"
72+
73+
-- 一次添加多个数据, 如: zadd set-name 0 a 1 b 等等
74+
local function mAdd(cmd, key, values)
75+
local args = {cmd, key};
76+
for i, v in ipairs(values) do
77+
table.insert(args, v);
78+
end
79+
return redis.call(unpack(args));
80+
end
3881

82+
local function randomScoreAndVal(len)
83+
local random = {}
84+
for i = 1, len do
85+
local score = math.random(0, 100);
86+
local key = 'value'..i;
87+
table.insert(random, score);
88+
table.insert(random, key);
89+
end
90+
return random;
91+
end
3992

93+
mAdd('zadd', 'zset', randomScoreAndVal(10));
94+
table.insert(redisTable, redis.call('zrange', 'zset', 0, 5, 'WITHSCORES'));
95+
-- ["value3","9","value1","17","value8","37","value5","58","value7","69","value10","75"], zset 随机添加, 所以输出值可能不同
4096

4197
--[[
4298
redis.pcall(), 执行命令出错, 会返回一个带 err 域的 lua table
99+
lua 脚本返回值只能是数组, 如果是 带域的 table 会被忽略
100+
脚本执行时,会被 redis 封装成一个 lua 函数进行执行
101+
redis 对 lua 有一定限制, 比如不能声明全局变量??
102+
--]]
43103

44-
{
45-
err:
46-
}
104+
table.insert(redisTable, redis.pcall('zrange')['err']);
105+
-- "@user_script: 109: Wrong number of args calling Redis command From Lua script"
47106

48-
--]]
49-
return {
50-
key,
51-
value,
52-
{table, boolean, number, str, luaNil}, -- nil 位置需注意
53-
{tk1, tk2, tv1, tv2},
54-
v1,
55-
factorial(10)
56-
};
107+
return retTable;

0 commit comments

Comments
 (0)