-
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.
Browse files
Browse the repository at this point in the history
[Fix] 일정 할당 동시성 제어
- Loading branch information
Showing
9 changed files
with
128 additions
and
21 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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/dnd/jjakkak/domain/redis/RedisRepository.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,37 @@ | ||
package com.dnd.jjakkak.domain.redis; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Redis 접근하는 Repository 클래스입니다. | ||
* | ||
* @author 정승조 | ||
* @version 2024. 10. 11. | ||
*/ | ||
@Repository | ||
@RequiredArgsConstructor | ||
public class RedisRepository { | ||
|
||
private final RedisTemplate<String, String> redisTemplate; | ||
|
||
public String findByKey(String key) { | ||
return redisTemplate.opsForValue().get(key); | ||
} | ||
|
||
public void save(String key, String value, Duration expiration) { | ||
redisTemplate.opsForValue().set(key, value, expiration); | ||
} | ||
|
||
public void delete(String key) { | ||
redisTemplate.delete(key); | ||
} | ||
|
||
public Boolean lock(String key) { | ||
return redisTemplate.opsForValue() | ||
.setIfAbsent(key, "lock", Duration.ofSeconds(5)); | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
src/main/java/com/dnd/jjakkak/domain/schedule/facade/ScheduleFacade.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.dnd.jjakkak.domain.schedule.facade; | ||
|
||
import com.dnd.jjakkak.domain.redis.RedisRepository; | ||
import com.dnd.jjakkak.domain.schedule.dto.request.ScheduleAssignRequestDto; | ||
import com.dnd.jjakkak.domain.schedule.dto.response.ScheduleAssignResponseDto; | ||
import com.dnd.jjakkak.domain.schedule.service.ScheduleService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* 일정 Facade 클래스입니다. | ||
* | ||
* @author 정승조 | ||
* @version 2024. 10. 11. | ||
*/ | ||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class ScheduleFacade { | ||
|
||
private final RedisRepository redisRepository; | ||
private final ScheduleService scheduleService; | ||
|
||
public void assignScheduleToMember(Long memberId, String meetingUuid, ScheduleAssignRequestDto requestDto) { | ||
String key = "meeting-" + meetingUuid; | ||
while (!redisRepository.lock(key)) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
try { | ||
scheduleService.assignScheduleToMember(memberId, meetingUuid, requestDto); | ||
} finally { | ||
redisRepository.delete(key); | ||
} | ||
} | ||
|
||
public ScheduleAssignResponseDto assignScheduleToGuest(String meetingUuid, ScheduleAssignRequestDto requestDto) { | ||
String key = "meeting-" + meetingUuid; | ||
while (!redisRepository.lock(key)) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
try { | ||
return scheduleService.assignScheduleToGuest(meetingUuid, requestDto); | ||
} finally { | ||
redisRepository.delete(key); | ||
} | ||
} | ||
} |
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