|
1 | 1 | package team.wego.wegobackend.group.v2.infrastructure.redis; |
2 | 2 |
|
3 | 3 | import java.time.Duration; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
4 | 6 | import java.util.Optional; |
| 7 | +import java.util.Set; |
5 | 8 | import lombok.RequiredArgsConstructor; |
6 | 9 | import org.springframework.data.redis.core.RedisTemplate; |
| 10 | +import org.springframework.data.redis.core.StringRedisTemplate; |
7 | 11 | import org.springframework.stereotype.Repository; |
8 | 12 | import team.wego.wegobackend.group.v2.application.dto.common.PreUploadedGroupImage; |
9 | 13 |
|
10 | 14 | @RequiredArgsConstructor |
11 | 15 | @Repository |
12 | 16 | public class PreUploadedGroupImageRedisRepository { |
13 | 17 |
|
14 | | - private static final Duration TTL = Duration.ofHours(2); |
| 18 | + // 메타데이터는 넉넉히: 1일 (GC, 장애 대비) |
| 19 | + private static final Duration META_TTL = Duration.ofDays(1); |
| 20 | + |
15 | 21 | private static final String PREFIX = "group:v2:img:pre:"; |
| 22 | + private static final String IDX_KEY = "group:v2:img:pre:idx"; |
16 | 23 |
|
17 | | - private final RedisTemplate<String, PreUploadedGroupImage> preUploadedGroupImageRedisTemplate; |
| 24 | + private final RedisTemplate<String, PreUploadedGroupImage> valueTemplate; |
| 25 | + private final StringRedisTemplate stringRedisTemplate; |
18 | 26 |
|
19 | 27 | private String key(String imageKey) { |
20 | 28 | return PREFIX + imageKey; |
21 | 29 | } |
22 | 30 |
|
23 | 31 | public void save(PreUploadedGroupImage value) { |
24 | | - preUploadedGroupImageRedisTemplate.opsForValue().set( |
25 | | - key(value.imageKey()), |
26 | | - value, |
27 | | - TTL |
28 | | - ); |
29 | | - } |
| 32 | + valueTemplate.opsForValue().set(key(value.imageKey()), value, META_TTL); |
30 | 33 |
|
31 | | - public Optional<PreUploadedGroupImage> find(String imageKey) { |
32 | | - PreUploadedGroupImage value = |
33 | | - preUploadedGroupImageRedisTemplate.opsForValue().get(key(imageKey)); |
34 | | - return Optional.ofNullable(value); |
| 34 | + long score = value.createdAt().atZone(java.time.ZoneId.systemDefault()).toEpochSecond(); |
| 35 | + stringRedisTemplate.opsForZSet().add(IDX_KEY, value.imageKey(), score); |
| 36 | + // IDX 자체 TTL은 옵션: 하루에 한 번 갱신해도 되고, 그냥 둬도 됩니다(멤버 정리로 관리). |
35 | 37 | } |
36 | 38 |
|
| 39 | + |
| 40 | + // 소비는 원자적으로 가져가면서 삭제 |
37 | 41 | public Optional<PreUploadedGroupImage> consume(String imageKey) { |
38 | 42 | PreUploadedGroupImage value = |
39 | | - preUploadedGroupImageRedisTemplate.opsForValue().getAndDelete(key(imageKey)); |
| 43 | + valueTemplate.opsForValue().getAndDelete(key(imageKey)); |
| 44 | + |
| 45 | + // 인덱스에서도 제거(있든 없든) |
| 46 | + stringRedisTemplate.opsForZSet().remove(IDX_KEY, imageKey); |
| 47 | + |
40 | 48 | return Optional.ofNullable(value); |
41 | 49 | } |
| 50 | + |
| 51 | + public Optional<PreUploadedGroupImage> find(String imageKey) { |
| 52 | + return Optional.ofNullable(valueTemplate.opsForValue().get(key(imageKey))); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + // 고아 이미지 삭제 전용: 특정 시간 이전 imageKey들 배치로 뽑기 |
| 57 | + public List<String> findExpiredCandidates(long thresholdEpochSec, int limit) { |
| 58 | + // ZRANGEBYSCORE idx -inf threshold LIMIT 0 limit |
| 59 | + Set<String> set = stringRedisTemplate.opsForZSet() |
| 60 | + .rangeByScore(IDX_KEY, Double.NEGATIVE_INFINITY, thresholdEpochSec, 0, limit); |
| 61 | + |
| 62 | + if (set == null || set.isEmpty()) { |
| 63 | + return List.of(); |
| 64 | + } |
| 65 | + return new ArrayList<>(set); |
| 66 | + } |
| 67 | + |
| 68 | + public void removeIndex(String imageKey) { |
| 69 | + stringRedisTemplate.opsForZSet().remove(IDX_KEY, imageKey); |
| 70 | + } |
42 | 71 | } |
43 | 72 |
|
0 commit comments