首页 > 代码库 > 关于使用 lua 脚本抢红包
关于使用 lua 脚本抢红包
1 java代码
package com.robert.RedisTest;import redis.clients.jedis.Jedis;public class RedisClient { public static void main(String[] args) { System.out.println(getHongBao()); System.out.println(); } public static void doAppend(StringBuilder script,String expression){ script.append(expression+"\n"); } public static int getHongBao(){ Jedis jedis = new Jedis("localhost", 6379); jedis.set("name","robert"); StringBuilder script = new StringBuilder(); doAppend(script,"local result = 0"); doAppend(script,"local afterDescNum = redis.call(‘decr‘, KEYS[1])"); doAppend(script,"if afterDescNum>=0 then"); doAppend(script,"result=1"); doAppend(script,"else "); doAppend(script,"result=0"); doAppend(script,"redis.call(‘set‘, KEYS[1], 0)"); doAppend(script,"end"); script.append("return result"); Object result = jedis.eval(script.toString(),1,"hongbaoNum"); int flag = Integer.parseInt(String.valueOf(result)); System.out.println(Thread.currentThread().getName()+" : "+flag); return flag; }}
qianghongbao.lua
local result = 0local afterDescNum = redis.call(‘decr‘, KEYS[1])if afterDescNum>=0 thenresult=1else result=0redis.call(‘set‘, KEYS[1], 0)endreturn result
关于使用 lua 脚本抢红包
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。