阿里云Redis云数据库,全面支持LUA脚本功能,助力企业轻松迁移自建Redis数据库的业务逻辑,实现业务的跨平台复用,快速驱动业务上云。LUA语言作为目前最流行的轻量级嵌入式脚本语言,凭借其语法简单、高效稳定、支持复杂数据结构以及自动内存管理等特点,已经在众多著名的游戏程序中大量被使用,如:愤怒的小鸟、星际争霸、魔兽世界等。
Redis支持LUA脚本的主要优势
Redis LUA脚本应用场景
if redis.call("EXISTS",KEYS[1]) == 1 then
return redis.call("INCRBY",KEYS[1],ARGV[1])
else
return nil
end
> evalsha f74dd5c086898b8a3d69655334b94fa7e006a9b1 1 activeusers 1 (nil)
> set activeusers 0 OK
> evalsha f74dd5c086898b8a3d69655334b94fa7e006a9b1 1 activeusers 1 (integer) 1
> evalsha f74dd5c086898b8a3d69655334b94fa7e006a9b1 1 activeusers 2 (integer) 3
local cnt = redis.call('INCR', KEYS[1])
if cnt > tonumber(ARGV[1])
then
return 1
end
if cnt == 1
then
redis.call('PEXPIRE', KEYS[1], ARGV[2])
end
return 0
> evalsha 089ccf077629d371793d5e928a3f06e9e483eb08 1 ratelimit:192.168.1.1 5 10000 (integer) 0
> evalsha 089ccf077629d371793d5e928a3f06e9e483eb08 1 ratelimit:192.168.1.1 5 10000 (integer) 0
> evalsha 089ccf077629d371793d5e928a3f06e9e483eb08 1 ratelimit:192.168.1.1 5 10000 (integer) 0
> evalsha 089ccf077629d371793d5e928a3f06e9e483eb08 1 ratelimit:192.168.1.1 5 10000 (integer) 0
> evalsha 089ccf077629d371793d5e928a3f06e9e483eb08 1 ratelimit:192.168.1.1 5 10000 (integer) 0
> evalsha 089ccf077629d371793d5e928a3f06e9e483eb08 1 ratelimit:192.168.1.1 5 10000 (integer) 1
for i=1,#KEYS do
if redis.call('sismember', KEYS[i], ARGV[1]) == 1 then
return 1
end
end
return 0
> sadd users alice bob (integer) 2
> sadd admin jenny (integer) 1
> evalsha d7550c872f553141096d5134c027af5eeed283db 2 users admin alice (integer) 1
> evalsha d7550c872f553141096d5134c027af5eeed283db 3 users admin guests jenny (integer) 1
> evalsha d7550c872f553141096d5134c027af5eeed283db 2 users admin abner (integer) 0
local fields = redis.call("SMEMBERS", KEYS[2])
local values = redis.call("HMGET", KEYS[1], unpack(fields))
local result = {}
for i,k in ipairs(fields) do result[i] = {k, values[i]} end
return result
> hset groceries bread 2 (integer) 1
> hset groceries apples 5 (integer) 1
> hset groceries oranges 6 (integer) 1
> hset groceries broccoli 1 (integer) 1
> sadd produce apples oranges broccoli (integer) 3
> evalsha 700c06c5ce9835bf9eef2198c8bc4d268b3b5095 2 groceries produce
1) 1) "apples"
2) "5"
2) 1) "broccoli"
2) "1"
3) 1) "oranges"
2) "6"
local currentval = tonumber(redis.call('get', KEYS[1])) or 0
local count = redis.call('incr', KEYS[2])
currentval = tostring(currentval * (count - 1)/count + (ARGV[1]/count))
redis.call('set', KEYS[1], currentval)
return currentval
> evalsha 399fddde578fd9cb924edce746c783e8340d8251 2 score:avg score:count 80 "80"
> evalsha 399fddde578fd9cb924edce746c783e8340d8251 2 score:avg score:count 100 "90"
> evalsha 399fddde578fd9cb924edce746c783e8340d8251 2 score:avg score:count 75 "85"
> evalsha 399fddde578fd9cb924edce746c783e8340d8251 2 score:avg score:count 98 "88.25"
> evalsha 399fddde578fd9cb924edce746c783e8340d8251 2 score:avg score:count 98 "90.2"
> evalsha 399fddde578fd9cb924edce746c783e8340d8251 2 score:avg score:count 98 "91.5"
> get score:avg "91.5"
> get score:count "6"
©2024 aliyunhn.com. All Rights Reserved 中科九洲科技股份有限公司-阿里云河南授权服务中心 豫B2-20080032-14 统计