-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: 좌석 잠금 만료 시간 seatService에서 설정할 수 있도록 로직 수정 및 SeatLockService -> SeatLockManager로 class명 수정 * refactor: BookingQueueRepository -> BookingQueueManager * feat: Redis 명령어 래퍼 클래스 작성 -> RedisOperator * refactor: bookingQueue rename Repository -> Manager, Service 리팩터링 * refactor: rename seatLock Service -> Manager, SeatService 리팩터링 * refactor: SeatLockManager RedisOperator 적용 * refactor: BookingService, EventSeatStatus 리팩터링 * chore * fix: selectSeat 버그 수정 * feat: handleConnectException 추가 * fix: getOrderInQueue myOrder 계산식 수정
- Loading branch information
Showing
14 changed files
with
214 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
api/api-booking/src/main/java/com/pgms/apibooking/common/util/RedisOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.pgms.apibooking.common.util; | ||
|
||
import java.time.Duration; | ||
import java.util.function.Supplier; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.pgms.apibooking.common.exception.BookingException; | ||
import com.pgms.coredomain.domain.common.BookingErrorCode; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class RedisOperator { | ||
|
||
private final RedisTemplate<String, String> redisTemplate; | ||
|
||
public void setIfAbsent(String key, String value, Integer expirationSeconds) { | ||
Duration timeout = Duration.ofSeconds(expirationSeconds); | ||
tryOperation(() -> redisTemplate.opsForValue().setIfAbsent(key, value, timeout)); | ||
} | ||
|
||
public String get(String key) { | ||
return tryOperation(() -> redisTemplate.opsForValue().get(key)); | ||
} | ||
|
||
public void delete(String key) { | ||
tryOperation(() -> redisTemplate.delete(key)); | ||
} | ||
|
||
public void addToZSet(String key, String value, double score) { | ||
tryOperation(() -> redisTemplate.opsForZSet().add(key, value, score)); | ||
} | ||
|
||
public Long getRankFromZSet(String key, String value) { | ||
return tryOperation(() -> redisTemplate.opsForZSet().rank(key, value)); | ||
} | ||
|
||
public void removeElementFromZSet(String key, String value) { | ||
tryOperation(() -> redisTemplate.opsForZSet().remove(key, value)); | ||
} | ||
|
||
public void removeRangeByScoreFromZSet(String key, double minScore, double maxScore) { | ||
tryOperation(() -> redisTemplate.opsForZSet().removeRangeByScore(key, minScore, maxScore)); | ||
} | ||
|
||
private <T> T tryOperation(Supplier<T> operation) { | ||
try { | ||
return operation.get(); | ||
} catch (Exception e) { | ||
log.error("RedisManager occurred: " + e.getMessage(), e); | ||
throw new BookingException(BookingErrorCode.SERVICE_UNAVAILABLE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
.../main/java/com/pgms/apibooking/domain/bookingqueue/repository/BookingQueueRepository.java
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
...ng/src/main/java/com/pgms/apibooking/domain/bookingqueue/service/BookingQueueManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.pgms.apibooking.domain.bookingqueue.service; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.pgms.apibooking.common.util.RedisOperator; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class BookingQueueManager { | ||
|
||
private final RedisOperator redisOperator; | ||
|
||
public void add(Long eventId, String sessionId, double currentTimeSeconds) { | ||
redisOperator.addToZSet(String.valueOf(eventId), sessionId, currentTimeSeconds); | ||
} | ||
|
||
public Optional<Long> getRank(Long eventId, String sessionId) { | ||
Long rank = redisOperator.getRankFromZSet(String.valueOf(eventId), sessionId); | ||
return Optional.ofNullable(rank); | ||
} | ||
|
||
public void remove(Long eventId, String sessionId) { | ||
redisOperator.removeElementFromZSet(String.valueOf(eventId), sessionId); | ||
} | ||
|
||
public void removeRangeByScore(Long eventId, double minScore, double maxScore) { | ||
redisOperator.removeRangeByScoreFromZSet(String.valueOf(eventId), minScore, maxScore); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
api/api-booking/src/main/java/com/pgms/apibooking/domain/seat/service/SeatLockManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.pgms.apibooking.domain.seat.service; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.pgms.apibooking.common.util.RedisOperator; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SeatLockManager { | ||
|
||
private final static String SEAT_LOCK_CACHE_KEY_PREFIX = "seatId:"; | ||
private final static String SEAT_LOCK_CACHE_VALUE_PREFIX = "memberId:"; | ||
|
||
private final RedisOperator redisOperator; | ||
|
||
public Optional<Long> getSelectorId(Long seatId) { | ||
String key = generateSeatLockKey(seatId); | ||
String value = redisOperator.get(key); | ||
return Optional.ofNullable(value == null ? null : extractMemberId(value)); | ||
} | ||
|
||
public void lockSeat(Long seatId, Long memberId, Integer expirationSeconds) { | ||
String key = generateSeatLockKey(seatId); | ||
String value = generateSeatLockValue(memberId); | ||
redisOperator.setIfAbsent(key, value, expirationSeconds); | ||
} | ||
|
||
public void unlockSeat(Long seatId) { | ||
redisOperator.delete(generateSeatLockKey(seatId)); | ||
} | ||
|
||
private String generateSeatLockKey(Long seatId) { | ||
return SEAT_LOCK_CACHE_KEY_PREFIX + seatId; | ||
} | ||
|
||
private String generateSeatLockValue(Long memberId) { | ||
return SEAT_LOCK_CACHE_VALUE_PREFIX + memberId; | ||
} | ||
|
||
private Long extractMemberId(String value) { | ||
return Long.parseLong(value.replace(SEAT_LOCK_CACHE_VALUE_PREFIX, "")); | ||
} | ||
} |
Oops, something went wrong.