local key = KEYS[1] --限流KEY(一秒一个) local limit = tonumber(ARGV[1]) --限流大小 local current = tonumber(redis.call('get', key) or "0") if current + 1 > limit then --若是超出限流大小 return 0 else --请求数+1,并设置2秒过时 redis.call("INCRBY", key,"1") redis.call("expire", key,"2") return 1 end
D:\Redis-x64-3.2.100 my.lua
D:\Redis-x64-3.2.100>redis-cli.exe --eval my5.lua 1 ip:1213 , 5
上面命令解释来源:https://www.redisgreen.net/blog/intro-to-lua-for-redis-programmers/
redis-cli.exe --eval my5.lua 1 ip:1213 , 5
上面命令中的1表示有几个key这里只有一个key 因此lua代码中只能用KEY[1]获取
KEY[1]=ip:1213
逗号,表示分割KEY和ARGV
再看一个例子:
local link_id = redis.call("INCR", KEYS[1]) redis.call("HSET", KEYS[2], link_id, ARGV[1]) return link_id redis-cli --eval incrset.lua links:counter links:urls , http://malcolmgladwellbookgenerator.com/
终于在国外一个大神写的call函数解决我困惑一天的问题
local redis = require 'redis' -- If you have some different host/port change it here local host = "127.0.0.1" local port = 6379 client = redis.connect(host, port) -- Workaround for absence of redis.call or atelast I did not find one -- And did not want to digg in redis source code to see how does he get redis.call redis.call = function(cmd, ...) return assert(loadstring('return client:'.. string.lower(cmd) ..'(...)'))(...) end local r = redis.call('get', 'foo') print(r)
有个这个call函数就能够方便调试了有个这个call函数就能够方便调试了有个这个call函数就能够方便调试了