-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: 사용자 정보를 레디스 대기열에 보관할 수 있다. (#58)
- Loading branch information
Showing
27 changed files
with
1,066 additions
and
40 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
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
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
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
4 changes: 3 additions & 1 deletion
4
src/main/java/com/thirdparty/ticketing/domain/waiting/room/WaitingCounter.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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package com.thirdparty.ticketing.domain.waiting.room; | ||
|
||
import com.thirdparty.ticketing.domain.waiting.WaitingMember; | ||
|
||
public interface WaitingCounter { | ||
|
||
/** | ||
* @return 사용자에게 부여되는 고유한 카운트를 반환한다. | ||
*/ | ||
long getNextCount(Long performanceId); | ||
long getNextCount(WaitingMember waitingMember); | ||
} |
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
58 changes: 58 additions & 0 deletions
58
src/main/java/com/thirdparty/ticketing/global/config/WaitingConfig.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,58 @@ | ||
package com.thirdparty.ticketing.global.config; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.thirdparty.ticketing.domain.waiting.manager.WaitingManager; | ||
import com.thirdparty.ticketing.domain.waiting.room.RunningRoom; | ||
import com.thirdparty.ticketing.domain.waiting.room.WaitingCounter; | ||
import com.thirdparty.ticketing.domain.waiting.room.WaitingLine; | ||
import com.thirdparty.ticketing.domain.waiting.room.WaitingRoom; | ||
import com.thirdparty.ticketing.global.waiting.manager.RedisWaitingManager; | ||
import com.thirdparty.ticketing.global.waiting.room.RedisRunningRoom; | ||
import com.thirdparty.ticketing.global.waiting.room.RedisWaitingCounter; | ||
import com.thirdparty.ticketing.global.waiting.room.RedisWaitingLine; | ||
import com.thirdparty.ticketing.global.waiting.room.RedisWaitingRoom; | ||
|
||
@Configuration | ||
public class WaitingConfig { | ||
|
||
@Bean | ||
public WaitingManager waitingManager( | ||
RunningRoom runningRoom, | ||
WaitingRoom waitingRoom, | ||
@Qualifier("lettuceRedisTemplate") StringRedisTemplate redisTemplate) { | ||
return new RedisWaitingManager(runningRoom, waitingRoom, redisTemplate); | ||
} | ||
|
||
@Bean | ||
public WaitingRoom waitingRoom( | ||
WaitingLine waitingLine, | ||
WaitingCounter waitingCounter, | ||
@Qualifier("lettuceRedisTemplate") StringRedisTemplate redisTemplate, | ||
ObjectMapper objectMapper) { | ||
return new RedisWaitingRoom(waitingLine, waitingCounter, redisTemplate, objectMapper); | ||
} | ||
|
||
@Bean | ||
public WaitingLine waitingLine( | ||
ObjectMapper objectMapper, | ||
@Qualifier("lettuceRedisTemplate") StringRedisTemplate redisTemplate) { | ||
return new RedisWaitingLine(objectMapper, redisTemplate); | ||
} | ||
|
||
@Bean | ||
public WaitingCounter waitingCounter( | ||
@Qualifier("lettuceRedisTemplate") StringRedisTemplate redisTemplate) { | ||
return new RedisWaitingCounter(redisTemplate); | ||
} | ||
|
||
@Bean | ||
public RunningRoom runningRoom( | ||
@Qualifier("lettuceRedisTemplate") StringRedisTemplate redisTemplate) { | ||
return new RedisRunningRoom(redisTemplate); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/thirdparty/ticketing/global/waiting/ObjectMapperUtils.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,25 @@ | ||
package com.thirdparty.ticketing.global.waiting; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.thirdparty.ticketing.domain.common.ErrorCode; | ||
import com.thirdparty.ticketing.domain.common.TicketingException; | ||
|
||
public class ObjectMapperUtils { | ||
|
||
public static String writeValueAsString(ObjectMapper objectMapper, Object value) { | ||
try { | ||
return objectMapper.writeValueAsString(value); | ||
} catch (JsonProcessingException e) { | ||
throw new TicketingException(ErrorCode.WAITING_WRITE_ERROR); | ||
} | ||
} | ||
|
||
public static <T> T readValue(ObjectMapper objectMapper, String value, Class<T> valueType) { | ||
try { | ||
return objectMapper.readValue(value, valueType); | ||
} catch (JsonProcessingException e) { | ||
throw new TicketingException(ErrorCode.WAITING_READ_ERROR); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/thirdparty/ticketing/global/waiting/manager/RedisWaitingManager.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.thirdparty.ticketing.global.waiting.manager; | ||
|
||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
|
||
import com.thirdparty.ticketing.domain.waiting.WaitingMember; | ||
import com.thirdparty.ticketing.domain.waiting.manager.WaitingManager; | ||
import com.thirdparty.ticketing.domain.waiting.room.RunningRoom; | ||
import com.thirdparty.ticketing.domain.waiting.room.WaitingRoom; | ||
|
||
public class RedisWaitingManager extends WaitingManager { | ||
|
||
private static final String MANAGED_MEMBER_COUNTER_KEY = "managed_member_counter:"; | ||
|
||
private final ValueOperations<String, String> managedMemberCounter; | ||
|
||
public RedisWaitingManager( | ||
RunningRoom runningRoom, WaitingRoom waitingRoom, StringRedisTemplate redisTemplate) { | ||
super(runningRoom, waitingRoom); | ||
managedMemberCounter = redisTemplate.opsForValue(); | ||
} | ||
|
||
@Override | ||
protected long countManagedMember(WaitingMember waitingMember) { | ||
String key = getPerformanceManagedMemberCounterKey(waitingMember); | ||
managedMemberCounter.setIfAbsent(key, "0"); // todo: 불필요하게 네트워크를 탐. 추후 개선 필요 | ||
return Long.parseLong(managedMemberCounter.get(key)); | ||
} | ||
|
||
private String getPerformanceManagedMemberCounterKey(WaitingMember waitingMember) { | ||
return MANAGED_MEMBER_COUNTER_KEY + waitingMember.getPerformanceId(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/thirdparty/ticketing/global/waiting/room/RedisRunningRoom.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.thirdparty.ticketing.global.waiting.room; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.SetOperations; | ||
|
||
import com.thirdparty.ticketing.domain.waiting.WaitingMember; | ||
import com.thirdparty.ticketing.domain.waiting.room.RunningRoom; | ||
|
||
public class RedisRunningRoom implements RunningRoom { | ||
|
||
private static final String RUNNING_ROOM_KEY = "running_room:"; | ||
|
||
private final SetOperations<String, String> runningRoom; | ||
|
||
public RedisRunningRoom(RedisTemplate<String, String> redisTemplate) { | ||
runningRoom = redisTemplate.opsForSet(); | ||
} | ||
|
||
@Override | ||
public boolean contains(WaitingMember waitingMember) { | ||
return runningRoom.isMember( | ||
getPerformanceRunningRoomKey(waitingMember), waitingMember.getEmail()); | ||
} | ||
|
||
@Override | ||
public void put(long performanceId, List<WaitingMember> waitingMembers) {} | ||
|
||
private String getPerformanceRunningRoomKey(WaitingMember waitingMember) { | ||
return RUNNING_ROOM_KEY + waitingMember.getPerformanceId(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/thirdparty/ticketing/global/waiting/room/RedisWaitingCounter.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,25 @@ | ||
package com.thirdparty.ticketing.global.waiting.room; | ||
|
||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
|
||
import com.thirdparty.ticketing.domain.waiting.WaitingMember; | ||
import com.thirdparty.ticketing.domain.waiting.room.WaitingCounter; | ||
|
||
public class RedisWaitingCounter implements WaitingCounter { | ||
|
||
private static final String WAITING_COUNTER_KEY = "waiting_counter"; | ||
|
||
private final ValueOperations<String, String> counter; | ||
|
||
public RedisWaitingCounter(StringRedisTemplate redisTemplate) { | ||
this.counter = redisTemplate.opsForValue(); | ||
} | ||
|
||
@Override | ||
public long getNextCount(WaitingMember waitingMember) { | ||
String performanceWaitingCounterKey = | ||
WAITING_COUNTER_KEY + waitingMember.getPerformanceId(); | ||
return counter.increment(performanceWaitingCounterKey, 1); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/thirdparty/ticketing/global/waiting/room/RedisWaitingLine.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,38 @@ | ||
package com.thirdparty.ticketing.global.waiting.room; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.data.redis.core.ZSetOperations; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.thirdparty.ticketing.domain.waiting.WaitingMember; | ||
import com.thirdparty.ticketing.domain.waiting.room.WaitingLine; | ||
import com.thirdparty.ticketing.global.waiting.ObjectMapperUtils; | ||
|
||
public class RedisWaitingLine implements WaitingLine { | ||
|
||
private static final String WAITING_LINE_KEY = "waiting_line:"; | ||
|
||
private final ObjectMapper objectMapper; | ||
private final ZSetOperations<String, String> waitingLine; | ||
|
||
public RedisWaitingLine(ObjectMapper objectMapper, StringRedisTemplate redisTemplate) { | ||
this.objectMapper = objectMapper; | ||
this.waitingLine = redisTemplate.opsForZSet(); | ||
} | ||
|
||
@Override | ||
public void enter(WaitingMember waitingMember) { | ||
String performanceWaitingLineKey = WAITING_LINE_KEY + waitingMember.getPerformanceId(); | ||
String waitingMemberValue = | ||
ObjectMapperUtils.writeValueAsString(objectMapper, waitingMember); | ||
waitingLine.add( | ||
performanceWaitingLineKey, waitingMemberValue, waitingMember.getWaitingCount()); | ||
} | ||
|
||
@Override | ||
public List<WaitingMember> pollWaitingMembers(long performanceId, long count) { | ||
return List.of(); | ||
} | ||
} |
Oops, something went wrong.