Uninote
Uninote
用户根目录
工作日志
local function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
   local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
   if not nFindLastIndex then
    nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
    break
   end
   nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
   nFindStartIndex = nFindLastIndex + string.len(szSeparator)
   nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end

-- 请求参数4个值,第一个:key,第二个:要设置的值,第三个:签名值

-- 设置值的时候先获取值,验证是否相等,如果相等就返回错误,让客户端重新获取然后设置,并且设置时间,如果时间是小于key里面的时间,那么就是错误的值
local result = redis.call('get', KEYS[1])

-- 如果没有设置过,就直接设置值
if (not result) then
	return redis.call('set', KEYS[1], ARGV[1])
end

-- 取出时间
local timesResult = Split(result, ',')[2]

-- 如果传进来的参数和获取到的一样,那么就重新获取
if (timesResult == ARGV[2]) then
	return redis.call('set', KEYS[1], ARGV[1])
else
	return 0
end

mac binlog

redis使用lua进行限流基本实现

点赞(0) 阅读(227) 举报
目录
标题