Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis 분산락 개발 #4

Closed
3 tasks done
leechoongyon opened this issue Apr 3, 2023 · 0 comments
Closed
3 tasks done

Redis 분산락 개발 #4

leechoongyon opened this issue Apr 3, 2023 · 0 comments

Comments

@leechoongyon
Copy link
Owner

leechoongyon commented Apr 3, 2023

개요

  • SETNX 를 통해 KEY 를 저장한 후, DEL 명령어를 통해 락을 해제한다.
  • 2개의 명령어는 원자성을 보장하는 명령어이다.

할일

  • 패키지 위치를 어떻게 잡아야지?
  • 어느 부분에 annotation 을 걸어야 하나?
  • 테스트는 어떻게 하지?

source

@Component
class DistributedLock(
    private val reactiveRedisTemplate: ReactiveRedisTemplate<String, String>,
    @Value("\${redis.lock.timeout}") private val timeout: Long
) {
    fun acquireLock(key: String): Mono<Boolean> {
        // Redis의 SETNX 명령어를 사용하여 락을 획득합니다. (원자성 보장)
        // key : 저장할 키, "" : 저장 value, timeout : TTL 
        return reactiveRedisTemplate.opsForValue().setIfAbsent(key, "", Duration.ofMillis(timeout))
    }
    fun releaseLock(key: String): Mono<Boolean> {
        // Redis의 DEL 명령어를 사용하여 락을 해제합니다. (원자성 보장)
        return reactiveRedisTemplate.delete(key)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant