Skip to content

Commit

Permalink
Merge pull request #21 from kjungw1025/fix/redisTicket
Browse files Browse the repository at this point in the history
fix: 기존 티켓팅 시스템에서 티켓 발급 순서가 밀리는 문제 수정
  • Loading branch information
kjungw1025 authored Apr 9, 2024
2 parents 2415bd8 + bb669dd commit df090f5
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,23 @@ public int enroll(Long userId, Long ticketEventId, Duration expiresNextKeyAfter)
throw new RuntimeException("It waited for 20 seconds, but can't acquire lock");

String nextIdKey = RedisKeys.combine(TICKET_NEXT_KEY, ticketEventId);
Long size = redisTemplate.opsForValue().increment(nextIdKey);

if (size == null) {
size = 1L;
redisTemplate.opsForValue().set(nextIdKey, size.toString());
}
redisTemplate.expire(nextIdKey, expiresNextKeyAfter);

if (!redisTemplate.opsForHash().putIfAbsent(key, userId.toString(), Long.toString(size))) {
if (redisTemplate.opsForHash().hasKey(key, userId.toString())) {
throw new AlreadyRequestedTicketException();
} else {
Long size = redisTemplate.opsForValue().increment(nextIdKey);

if (size == null) {
size = 1L;
redisTemplate.opsForValue().set(nextIdKey, size.toString());
}
redisTemplate.expire(nextIdKey, expiresNextKeyAfter);

redisTemplate.opsForHash().put(key, userId.toString(), Long.toString(size));
redisTemplate.opsForSet().add(TICKET_RESERVATION_SET_KEY, ticketEventId.toString());
}

return size.intValue();
return size.intValue();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
Expand Down

0 comments on commit df090f5

Please sign in to comment.