-
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.
- Loading branch information
Showing
11 changed files
with
144 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
services: | ||
redis: | ||
container_name: fluffy-redis | ||
image: redis:7.4.1 | ||
restart: always | ||
ports: | ||
- '6379:6379' |
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
21 changes: 21 additions & 0 deletions
21
server/src/main/java/com/fluffy/global/cache/LockManager.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,21 @@ | ||
package com.fluffy.global.cache; | ||
|
||
import java.time.Duration; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class LockManager { | ||
|
||
private final RedisTemplate<String, String> redisTemplate; | ||
|
||
public Boolean lock(String key) { | ||
return redisTemplate.opsForValue().setIfAbsent(key, "lock", Duration.ofSeconds(3)); | ||
} | ||
|
||
public Boolean unlock(String key) { | ||
return redisTemplate.delete(key); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
server/src/main/java/com/fluffy/global/cache/RedisConfig.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,30 @@ | ||
package com.fluffy.global.cache; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
private String redisHost; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int redisPort; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(redisHost, redisPort); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate() { | ||
RedisTemplate<String, Object> template = new RedisTemplate<>(); | ||
template.setConnectionFactory(redisConnectionFactory()); | ||
return template; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
server/src/main/java/com/fluffy/global/cache/RedisLockUtil.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,51 @@ | ||
package com.fluffy.global.cache; | ||
|
||
import java.util.function.Supplier; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class RedisLockUtil { | ||
|
||
private final LockManager manager; | ||
|
||
public <T> T acquireAndRunLock(String key, Supplier<T> block) { | ||
if (key == null || key.isBlank()) { | ||
throw new IllegalArgumentException("[RedisLock] Key cannot be null or empty"); | ||
} | ||
|
||
if (Boolean.TRUE.equals(acquireLock(key))) { | ||
return proceedWithLock(key, block); | ||
} | ||
|
||
throw new IllegalStateException("[RedisLock] Failed to acquire lock for key: " + key); | ||
} | ||
|
||
private Boolean acquireLock(String key) { | ||
try { | ||
return manager.lock(key); | ||
} catch (Exception e) { | ||
log.error("[RedisLock] Failed to acquire lock for key: {}", key, e); | ||
return false; | ||
} | ||
} | ||
|
||
private <T> T proceedWithLock(String key, Supplier<T> block) { | ||
try { | ||
return block.get(); | ||
} finally { | ||
releaseLock(key); | ||
} | ||
} | ||
|
||
private void releaseLock(String key) { | ||
try { | ||
manager.unlock(key); | ||
} catch (Exception e) { | ||
log.error("[RedisLock] Failed to release lock for key: {}", key, e); | ||
} | ||
} | ||
} |
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