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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| @Service public class CompleteLotteryService { @Autowired private JedisPool jedisPool; @Autowired private RedissonClient redissonClient;
public String createLottery(LotteryModel lottery) { String lotteryId = UUID.randomUUID().toString(); try (Jedis jedis = jedisPool.getResource()) { String key = "lottery:config:" + lotteryId; jedis.hset(key, "name", lottery.getName()); jedis.hset(key, "status", "NOT_STARTED"); jedis.hset(key, "startTime", String.valueOf(lottery.getStartTime().getTime())); jedis.hset(key, "endTime", String.valueOf(lottery.getEndTime().getTime())); String prizesJson = JSON.toJSONString(lottery.getPrizes()); jedis.hset(key, "prizes", prizesJson); } return lotteryId; }
public void participate(String lotteryId, String userId) { String key = "lottery:participants:" + lotteryId; try (Jedis jedis = jedisPool.getResource()) { if (jedis.sismember("lottery:users:" + lotteryId, userId)) { throw new RuntimeException("用户已参与"); } jedis.sadd("lottery:users:" + lotteryId, userId); jedis.rpush(key, userId); } }
public void startLottery(String lotteryId) { RLock lock = redissonClient.getLock("lottery:lock:" + lotteryId); try { if (lock.tryLock(30, 10, TimeUnit.SECONDS)) { try { List<String> participants = getAllParticipants(lotteryId); Collections.shuffle(participants); updateShuffledParticipants(lotteryId, participants); updateLotteryStatus(lotteryId, "ONGOING"); } finally { lock.unlock(); } } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
public List<String> draw(String lotteryId, int count) { try (Jedis jedis = jedisPool.getResource()) { String key = "lottery:participants:" + lotteryId; String script = "local results = {} " + "for i = 1, ARGV[1] do " + " local userId = redis.call('lpop', KEYS[1]) " + " if userId then " + " table.insert(results, userId) " + " end " + "end " + "return results"; @SuppressWarnings("unchecked") List<String> winners = (List<String>) jedis.eval(script, Collections.singletonList(key), Collections.singletonList(String.valueOf(count)) ); return winners; } } private List<String> getAllParticipants(String lotteryId) { try (Jedis jedis = jedisPool.getResource()) { String key = "lottery:participants:" + lotteryId; return jedis.lrange(key, 0, -1); } } private void updateShuffledParticipants(String lotteryId, List<String> participants) { try (Jedis jedis = jedisPool.getResource()) { String key = "lottery:participants:" + lotteryId; jedis.del(key); Pipeline pipeline = jedis.pipelined(); for (String userId : participants) { pipeline.rpush(key, userId); } pipeline.sync(); } } private void updateLotteryStatus(String lotteryId, String status) { try (Jedis jedis = jedisPool.getResource()) { String key = "lottery:config:" + lotteryId; jedis.hset(key, "status", status); } } }
|