windows redis+lua的调试

1.编写lua脚本my.lua

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

  

2.把写好的lua脚本my.lua放到windows redis目录下面就是和redis-cli.exe同级目录

D:\Redis-x64-3.2.100
   my.lua

  

3.打开windows cmd命令窗口执行命令

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函数就可以方便调试了


 

 

 

转载于:https://www.cnblogs.com/fofawubian/p/8192883.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值