-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Cache stampede 문제 Lock 으로 해결 (#193)
* chore: redisson 설정 추가 * feat: redisson 분산락 기능 구현 * feat: 로그 및 에러 수정
- Loading branch information
Showing
6 changed files
with
89 additions
and
13 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
33 changes: 33 additions & 0 deletions
33
src/main/java/cotato/bookitlist/book/redis/RedissonLockService.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 cotato.bookitlist.book.redis; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class RedissonLockService { | ||
private static final String LOCK_PREFIX = "LOCK"; | ||
private final RedissonClient redissonClient; | ||
|
||
private static String getLockKey(String domain, String key) { | ||
return LOCK_PREFIX + ":" + domain + ":" + key; | ||
} | ||
|
||
public boolean tryLock(String domain, Object key) throws InterruptedException { | ||
RLock lock = redissonClient.getLock(getLockKey(domain, key.toString())); | ||
log.debug("tryLock domain={} key={}", domain, key); | ||
return lock.tryLock(500, 10000, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
public void unlock(String domain, Object key) { | ||
RLock lock = redissonClient.getLock(getLockKey(domain, key.toString())); | ||
log.debug("unlock domain={} key={}", domain, key); | ||
lock.unlock(); | ||
} | ||
} |
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