11package com .loopers .application .product ;
22
3+ import com .loopers .config .redis .RedisConfig ;
34import com .loopers .domain .product .Product ;
45import com .loopers .domain .product .ProductSearchCondition ;
56import com .loopers .domain .product .ProductService ;
6- import lombok .RequiredArgsConstructor ;
7+ import org .springframework .beans .factory .annotation .Qualifier ;
8+ import org .springframework .beans .factory .annotation .Value ;
79import org .springframework .data .domain .Page ;
810import org .springframework .data .redis .core .RedisTemplate ;
911import org .springframework .data .redis .core .ScanOptions ;
1012import org .springframework .stereotype .Service ;
13+
1114import java .time .Duration ;
1215import java .util .HashSet ;
1316import java .util .Map ;
1417import java .util .Set ;
1518import java .util .stream .Collectors ;
1619
1720@ Service
18- @ RequiredArgsConstructor
1921public class ProductCacheService {
2022
2123 private final ProductService productService ;
2224 private final RedisTemplate <String , Object > redisTemplate ;
2325
24- private static final String CACHE_VERSION = "v1" ;
25- private static final String PRODUCT_DETAIL_KEY_PREFIX = "product:" + CACHE_VERSION + ":detail:" ;
26- private static final String PRODUCT_LIST_KEY_PREFIX = "product:" + CACHE_VERSION + ":list:" ;
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+
2737 private static final Duration DETAIL_TTL = Duration .ofMinutes (10 );
2838 private static final Duration LIST_TTL = Duration .ofMinutes (5 );
2939
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+
3048 public ProductDetailInfo getProductDetailWithCache (Long productId ) {
31- String key = PRODUCT_DETAIL_KEY_PREFIX + productId ;
49+ String key = productDetailKeyPrefix () + productId ;
3250
3351 Object cached = redisTemplate .opsForValue ().get (key );
3452 if (cached instanceof ProductDetailInfo info ) {
@@ -44,7 +62,7 @@ public ProductDetailInfo getProductDetailWithCache(Long productId) {
4462 }
4563
4664 public ProductListInfo getProductListWithCache (String cacheKey , ProductSearchCondition condition ) {
47- String key = PRODUCT_LIST_KEY_PREFIX + cacheKey ;
65+ String key = productListKeyPrefix () + cacheKey ;
4866
4967 Object cached = redisTemplate .opsForValue ().get (key );
5068 if (cached instanceof ProductListInfo info ) {
@@ -65,29 +83,29 @@ public ProductListInfo getProductListWithCache(String cacheKey, ProductSearchCon
6583 }
6684
6785 public void evictProductDetailCache (Long productId ) {
68- String key = PRODUCT_DETAIL_KEY_PREFIX + productId ;
86+ String key = productDetailKeyPrefix () + productId ;
6987 redisTemplate .delete (key );
7088 }
7189
7290 public void evictProductListCachesByLikesSort () {
73- String pattern = PRODUCT_LIST_KEY_PREFIX + "*:sort:likes_desc:*" ;
91+ String pattern = productListKeyPrefix () + "*:sort:likes_desc:*" ;
7492 Set <String > keys = scanRedisKeys (pattern );
7593 if (!keys .isEmpty ()) {
7694 redisTemplate .delete (keys );
7795 }
7896 }
7997
8098 public void evictAllProductListCaches () {
81- String pattern = PRODUCT_LIST_KEY_PREFIX + "*" ;
99+ String pattern = productListKeyPrefix () + "*" ;
82100 Set <String > keys = scanRedisKeys (pattern );
83101 if (!keys .isEmpty ()) {
84102 redisTemplate .delete (keys );
85103 }
86104 }
87105
88106 public void evictAllProductCaches () {
89- Set <String > detailKeys = scanRedisKeys (PRODUCT_DETAIL_KEY_PREFIX + "*" );
90- Set <String > listKeys = scanRedisKeys (PRODUCT_LIST_KEY_PREFIX + "*" );
107+ Set <String > detailKeys = scanRedisKeys (productDetailKeyPrefix () + "*" );
108+ Set <String > listKeys = scanRedisKeys (productListKeyPrefix () + "*" );
91109
92110 long deletedCount = 0 ;
93111 if (!detailKeys .isEmpty ()) {
0 commit comments