|
| 1 | +package com.loopers.application.product; |
| 2 | + |
| 3 | +import com.loopers.config.redis.RedisConfig; |
| 4 | +import com.loopers.domain.product.Product; |
| 5 | +import com.loopers.domain.product.ProductSearchCondition; |
| 6 | +import com.loopers.domain.product.ProductService; |
| 7 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.data.domain.Page; |
| 10 | +import org.springframework.data.redis.core.RedisTemplate; |
| 11 | +import org.springframework.data.redis.core.ScanOptions; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | + |
| 14 | +import java.time.Duration; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +@Service |
| 21 | +public class ProductCacheService { |
| 22 | + |
| 23 | + private final ProductService productService; |
| 24 | + private final RedisTemplate<String, Object> redisTemplate; |
| 25 | + |
| 26 | + @Value("${cache.version.product}") |
| 27 | + private String cacheVersion; |
| 28 | + |
| 29 | + private String productDetailKeyPrefix() { |
| 30 | + return "product:" + cacheVersion + ":detail:"; |
| 31 | + } |
| 32 | + |
| 33 | + private String productListKeyPrefix() { |
| 34 | + return "product:" + cacheVersion + ":list:"; |
| 35 | + } |
| 36 | + |
| 37 | + private static final Duration DETAIL_TTL = Duration.ofMinutes(10); |
| 38 | + private static final Duration LIST_TTL = Duration.ofMinutes(5); |
| 39 | + |
| 40 | + public ProductCacheService( |
| 41 | + ProductService productService, |
| 42 | + @Qualifier(RedisConfig.REDIS_TEMPLATE_CACHE) RedisTemplate<String, Object> redisTemplate |
| 43 | + ) { |
| 44 | + this.productService = productService; |
| 45 | + this.redisTemplate = redisTemplate; |
| 46 | + } |
| 47 | + |
| 48 | + public ProductDetailInfo getProductDetailWithCache(Long productId) { |
| 49 | + String key = productDetailKeyPrefix() + productId; |
| 50 | + |
| 51 | + Object cached = redisTemplate.opsForValue().get(key); |
| 52 | + if (cached instanceof ProductDetailInfo info) { |
| 53 | + return info; |
| 54 | + } |
| 55 | + |
| 56 | + Product product = productService.getProduct(productId); |
| 57 | + ProductDetailInfo info = ProductDetailInfo.of(product, product.getLikeCount()); |
| 58 | + |
| 59 | + redisTemplate.opsForValue().set(key, info, DETAIL_TTL); |
| 60 | + |
| 61 | + return info; |
| 62 | + } |
| 63 | + |
| 64 | + public ProductListInfo getProductListWithCache(String cacheKey, ProductSearchCondition condition) { |
| 65 | + String key = productListKeyPrefix() + cacheKey; |
| 66 | + |
| 67 | + Object cached = redisTemplate.opsForValue().get(key); |
| 68 | + if (cached instanceof ProductListInfo info) { |
| 69 | + return info; |
| 70 | + } |
| 71 | + |
| 72 | + Page<Product> productPage = productService.getProducts(condition); |
| 73 | + Map<Long, Long> likeCountMap = productPage.getContent().stream() |
| 74 | + .collect(Collectors.toMap( |
| 75 | + Product::getId, |
| 76 | + Product::getLikeCount |
| 77 | + )); |
| 78 | + ProductListInfo info = ProductListInfo.of(productPage, likeCountMap); |
| 79 | + |
| 80 | + redisTemplate.opsForValue().set(key, info, LIST_TTL); |
| 81 | + |
| 82 | + return info; |
| 83 | + } |
| 84 | + |
| 85 | + public void evictProductDetailCache(Long productId) { |
| 86 | + String key = productDetailKeyPrefix() + productId; |
| 87 | + redisTemplate.delete(key); |
| 88 | + } |
| 89 | + |
| 90 | + public void evictProductListCachesByLikesSort() { |
| 91 | + String pattern = productListKeyPrefix() + "*:sort:likes_desc:*"; |
| 92 | + Set<String> keys = scanRedisKeys(pattern); |
| 93 | + if (!keys.isEmpty()) { |
| 94 | + redisTemplate.delete(keys); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public void evictAllProductListCaches() { |
| 99 | + String pattern = productListKeyPrefix() + "*"; |
| 100 | + Set<String> keys = scanRedisKeys(pattern); |
| 101 | + if (!keys.isEmpty()) { |
| 102 | + redisTemplate.delete(keys); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void evictAllProductCaches() { |
| 107 | + Set<String> detailKeys = scanRedisKeys(productDetailKeyPrefix() + "*"); |
| 108 | + Set<String> listKeys = scanRedisKeys(productListKeyPrefix() + "*"); |
| 109 | + |
| 110 | + long deletedCount = 0; |
| 111 | + if (!detailKeys.isEmpty()) { |
| 112 | + deletedCount += redisTemplate.delete(detailKeys); |
| 113 | + } |
| 114 | + if (!listKeys.isEmpty()) { |
| 115 | + deletedCount += redisTemplate.delete(listKeys); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private Set<String> scanRedisKeys(String pattern) { |
| 120 | + Set<String> keys = new HashSet<>(); |
| 121 | + ScanOptions options = ScanOptions.scanOptions() |
| 122 | + .match(pattern) |
| 123 | + .count(100) |
| 124 | + .build(); |
| 125 | + |
| 126 | + try { |
| 127 | + var connectionFactory = redisTemplate.getConnectionFactory(); |
| 128 | + if (connectionFactory == null) { |
| 129 | + return keys; |
| 130 | + } |
| 131 | + var connection = connectionFactory.getConnection(); |
| 132 | + try (var cursor = connection.scan(options)) { |
| 133 | + while (cursor.hasNext()) { |
| 134 | + keys.add(new String(cursor.next())); |
| 135 | + } |
| 136 | + } |
| 137 | + } catch (Exception e) { |
| 138 | + return keys; |
| 139 | + } |
| 140 | + return keys; |
| 141 | + } |
| 142 | +} |
0 commit comments