1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
@PostMapping("/participate") public ResultMsg participate(@RequestParam("userId") Long userId , @RequestParam("commodityId") Long commodityId , @RequestParam("key") String key , HttpServletRequest request) {
BoundHashOperations<String, Object, Object> boundHashOps = redisTemplate.boundHashOps("active:kill");
String status = (String) boundHashOps.get(userId + ""); if (null == status) { if (addQueue(userId, commodityId)){ redisTemplate.opsForValue().set("active:kill:" + userId, "2", 1, TimeUnit.DAYS); status="2"; } else { redisTemplate.opsForValue().set("active:kill:" + userId, "1", 1, TimeUnit.SECONDS); status="1"; } }
ResultMsg msg = null; if ("2".equals(status)){ msg = new ResultMsg(200, true, "正在排队中"); } else if ("3".equals(status)){ msg = new ResultMsg(200, false, "购买成功"); } else { msg = new ResultMsg(402, false, "当前人数过多"); } request.setAttribute("body", JSON.toJSONString(msg)); request.setAttribute("key", key);
return msg; }
private boolean addQueue(Long userId, Long commodityId) { Long size = redisTemplate.boundListOps("active:exChange").size(); if (size >= 1000) { return false; } System.out.println("进入排队状态"); redisTemplate.opsForList().rightPush("active:exChange", userId + "_" + commodityId); return true; }
|