-
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.
feat: CacheSynchronizationService 추가 (#320)
- LocalCache 데이터를 주기적으로 Redis로 동기화 - 비동기 처리를 통해 성능 병목 현상 방지 - 배치 사이즈를 작게 설정하여 처리 성능 최적화
- Loading branch information
Showing
5 changed files
with
84 additions
and
47 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
66 changes: 66 additions & 0 deletions
66
...structure/src/main/java/com/infrastructure/cache/service/CacheSynchronizationService.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,66 @@ | ||
package com.infrastructure.cache.service; | ||
|
||
import com.infrastructure.cache.repository.LocalCacheRepository; | ||
import com.infrastructure.cache.repository.RedisCacheRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* CacheSynchronizationService | ||
* | ||
* 이 서비스는 Redis Image가 일시적으로 다운되어 Local Cache가 사용된 경우, | ||
* LocalCache 데이터를 주기적으로 Redis로 동기화하는 역할을 합니다. | ||
* | ||
* 주요 기능: | ||
* 1. 5분마다 LocalCache 데이터를 Redis로 동기화 | ||
* 2. 각 키를 비동기적으로 처리하여 성능 병목을 방지 | ||
*/ | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class CacheSynchronizationService { | ||
|
||
private final RedisCacheRepository redisCacheRepository; | ||
private final LocalCacheRepository localCacheRepository; | ||
|
||
/** | ||
* 5분마다 LocalCache 데이터를 Redis로 동기화 | ||
*/ | ||
@Scheduled(fixedRate = 300000) // 5분마다 실행 | ||
public void syncLocalCacheToRedis() { | ||
log.info("Starting batch synchronization of LocalCache to Redis..."); | ||
Set<String> allKeys = localCacheRepository.getAllKeys(); | ||
List<String> keyList = new ArrayList<>(allKeys); | ||
int batchSize = 10; // 한 번에 처리할 배치 크기 | ||
|
||
for (int i = 0; i < keyList.size(); i += batchSize) { | ||
List<String> batch = keyList.subList(i, Math.min(i + batchSize, keyList.size())); | ||
batch.forEach(this::syncKeyAsync); | ||
} | ||
} | ||
|
||
/** | ||
* 개별 키를 Redis로 비동기 동기화 | ||
*/ | ||
@Async | ||
public void syncKeyAsync(String key) { | ||
try { | ||
String value = localCacheRepository.get(key); | ||
if (value != null) { | ||
redisCacheRepository.set(key, value, 60); // TTL 60초 | ||
localCacheRepository.delete(key); // 동기화 후 LocalCache에서 제거 | ||
log.info("Key '{}' synchronized to Redis.", key); | ||
} | ||
} catch (Exception e) { | ||
log.warn("Failed to synchronize key '{}' to Redis: {}", key, e.getMessage()); | ||
} | ||
} | ||
} |
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: 0 additions & 33 deletions
33
ttoklip-infrastructure/src/main/java/com/infrastructure/util/RedisUtil.java
This file was deleted.
Oops, something went wrong.