lua+redis实现抢红包

创建红包

        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            map.put("id", "rid" + i);
            map.put("money", i +"");
            jedisClient.lpush("hongbaoPoolKey", JSONUtils.toJSONString(map));
        }

lua脚本

local userId = ARGV[1]
if redis.call("SISMEMBER", KEYS[3], userId) ==1 then
  return nil
else
  local hongbao = redis.call("RPOP", KEYS[1]);
  if hongbao then
    local x = cjson.decode(hongbao);
    x['userId'] = userId;
    local re = cjson.encode(x);
    redis.call("SADD", KEYS[3], userId)
    redis.call("LPUSH", KEYS[2], re)
    return re
  else
    return nil
  end
end

实现抢红包

public class Qianghongbao {

    private static JedisPool jedisPool = new JedisPool("192.168.27.128", 6379);

    private static AtomicInteger atomicInteger = new AtomicInteger(0);

    public static void main(String[] args) {
        String script = "local userId = ARGV[1]\n" +
                "if redis.call(\"SISMEMBER\", KEYS[3], userId) == 1 then\n" +
                "  return nil\n" +
                "else\n" +
                "  local hongbao = redis.call(\"RPOP\", KEYS[1]);\n" +
                "  if hongbao then\n" +
                "    local x = cjson.decode(hongbao);\n" +
                "    x['userId'] = userId;\n" +
                "    local re = cjson.encode(x);\n" +
                "    redis.call(\"SADD\", KEYS[3], userId)\n" +
                "    redis.call(\"LPUSH\", KEYS[2], re)\n" +
                "    return re\n" +
                "  else\n" +
                "    return nil\n" +
                "  end\n" +
                "end";
        int n = 10;
// 实现一起开始抢
        CyclicBarrier cyclicBarrier = new CyclicBarrier(n);
//抢完之后,统计多少人抢到了红包
        CyclicBarrier cyclicBarrier2 = new CyclicBarrier(n, new Runnable() {
            @Override
            public void run() {
                System.out.println("共" + atomicInteger.get() + "抢到了红包");
            }
        });
/*        System.out.println( resource.eval(script, 3, "hongbaoPoolKey", "hongbaoDetails",
                "userSetForGrabbed", "1234541"));*/
        for (int i = 0; i < n; i++) {
            if (i == 19) {
                System.out.println();
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Jedis resource = null;
                    try {
                        cyclicBarrier.await();
                       // 每个线程需要获取一个连接
                        resource = jedisPool.getResource();
                        String userId = UUID.randomUUID().toString();

                        Object eval = resource.eval(script, 3, "hongbaoPoolKey", "hongbaoDetails",
                                "userSetForGrabbed", userId);
                        if (eval != null) {
                            atomicInteger.addAndGet(1);
                            System.out.println(userId + "抢到了红包");
                        } else {
                            System.out.println(userId + "没有抢到红包");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (resource != null) {// 连接池最多有8个连接,用完记得关闭
                         resource.close();
                        }
                        try {
                            cyclicBarrier2.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (BrokenBarrierException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }).start();
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值