Skip to content

Commit 57fe5a7

Browse files
committed
fix: TTL CAS 로직 TOCTOU 이슈 수정
1 parent 376903f commit 57fe5a7

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

apps/commerce-streamer/src/main/java/com/loopers/infrastructure/cache/ProductRankingCache.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,14 @@ private void decrementScore(Long productId, double score) {
171171
}
172172

173173
/**
174-
* TTL 설정 (CAS로 원자적 처리)
175-
* AtomicReference를 사용하여 날짜별로 한 번만 설정 (Race Condition 방지)
174+
* TTL 설정 (원자적 처리)
175+
* getAndSet으로 키 변경 시에만 TTL 설정 (날짜가 바뀌는 경우)
176176
*/
177177
private void ensureTtl(String key) {
178-
if (!key.equals(ttlInitializedKey.get())) {
179-
if (ttlInitializedKey.compareAndSet(ttlInitializedKey.get(), key)) {
180-
cacheRedisTemplate.expire(key, rankingConfig.getTtlDays(), TimeUnit.DAYS);
181-
log.info("[Ranking] TTL set for key: {} ({} days)", key, rankingConfig.getTtlDays());
182-
}
178+
String oldKey = ttlInitializedKey.getAndSet(key);
179+
if (!key.equals(oldKey)) {
180+
cacheRedisTemplate.expire(key, rankingConfig.getTtlDays(), TimeUnit.DAYS);
181+
log.info("[Ranking] TTL set for key: {} ({} days)", key, rankingConfig.getTtlDays());
183182
}
184183
}
185184

0 commit comments

Comments
 (0)