package store.b2c.c.utils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Random; /** * 门店商品信息 */ @Component public class StoreProductCacheKeyService { private static String ALL_STORE_PRODUCT_LIST = "store_prd_%s"; //门店所有商品列表cache key private static String STORE_GROUP_IDS = "store_group_ids_%s"; //门店组列表cache key private static String STORE_PRODUCT_SALE_DATE = "store_prd_sale_date_%s"; //门店的销量和时间 cache KEY private static String STORE_PRODUCT_PRICE = "store_prd_price_%s"; //查询商品实时价格 cache KEY %s是商品skuid /** * 门店所有商品列表缓存时间 */ @Value("${store.product.cache.baseExpire:180}") private int baseExpire;//过期 秒 /** * 门店组时间 */ @Value("${store.product.cache.storeGroupIdsExpire:600}") private int storeGroupIdsExpire;//秒 /** * 销量和上架时间 */ @Value("${store.product.cache.storeProductSaleAndDateExpire:3600}") private int storeProductSaleAndDateExpire;//秒 /** * 商品实时价格缓存时间 */ @Value("${store.product.cache.storeProductPriceExpire:300}") private int storeProductPriceExpire;//秒 /** * 随机缓存 时间 */ @Value("${store.product.cache.randomExpire:60}") public int randomExpire;//随机过期 秒 @Value("${store.product.cache.open:true}") private Boolean open;//是否打开缓存 true打开 false关闭 private Random random = new Random(); public Boolean getOpen() { return open; } /** * 获取门店所有商品列表key * * @param storeId * @return */ public static String getAllStoreProductList(Long storeId) { return String.format(ALL_STORE_PRODUCT_LIST, storeId); } /** * 获取门店组缓存key * * @param storeId * @return */ public static String getStoreGroupIds(Long storeId) { return String.format(STORE_GROUP_IDS, storeId); } /** * 销量和上架时间 * * @param storeId * @return */ public static String getStoreProductSaleDate(Long storeId) { return String.format(STORE_PRODUCT_SALE_DATE, storeId); } /** * 商品实时价格缓存 * * @param skuId * @return */ public static String getStoreProductPrice(Long skuId) { return String.format(STORE_PRODUCT_PRICE, skuId); } /** * 获取所有门店商品 缓存时间 s * * @return */ public int getAllExpire() { return baseExpire + random.nextInt(randomExpire); } public int getStoreGroupIdsExpire() { return storeGroupIdsExpire + random.nextInt(randomExpire); } public int getStoreProductSaleAndDateExpire() { return storeProductSaleAndDateExpire + random.nextInt(randomExpire); } public int getStoreProductPriceExpire() { return storeProductPriceExpire + random.nextInt(randomExpire); } }
使用缓存
private void saveQuerySkuPriceCache(Map<Long, PriceDto> priceDtoMap) { if (!storeProductCacheKeyService.getOpen()) { return; } for (Map.Entry<Long, PriceDto> priceDtoEntry : priceDtoMap.entrySet()) { redisUtils.setStrEx(StoreProductCacheKeyService.getStoreProductPrice(priceDtoEntry.getKey()), JsonUtils.toJson(priceDtoEntry.getValue()), storeProductCacheKeyService.getStoreProductPriceExpire()); } } private List<Long> getNeedQuerySkuIdList(List<Long> skuIdList, Map<Long, PriceDto> priceDtoMap) { if (org.apache.commons.collections4.CollectionUtils.isEmpty(skuIdList) || MapUtils.isEmpty(priceDtoMap)) { return skuIdList; } return skuIdList.stream() .filter(skuId -> priceDtoMap.get(skuId) == null) .collect(Collectors.toList()); } private Map<Long, PriceDto> querySkuPriceFromCache(List<Long> skuIds) { Map<Long, PriceDto> cacheProductDetailDtoMap = new HashMap<>(10);//保存查询缓存数据中的商品数据 if (!storeProductCacheKeyService.getOpen()) { return cacheProductDetailDtoMap; } List<String> redisCacheKeyList = buildAllRedisCacheKey(skuIds); List<String> cacheValueList = redisUtils.mGetStr(redisCacheKeyList); if (log.isDebugEnabled()) { log.debug("querySkuPriceFromCache={}", JsonUtils.toJson(cacheValueList)); } if (org.apache.commons.collections.CollectionUtils.isNotEmpty(cacheValueList)) { for (String cacheValue : cacheValueList) { PriceDto priceDto = JsonUtils.fromJson(cacheValue, PriceDto.class); if (priceDto != null) { cacheProductDetailDtoMap.put(priceDto.getSkuId(), priceDto); } } } return cacheProductDetailDtoMap; } private List<String> buildAllRedisCacheKey(List<Long> skuIdList) { List<String> redisCacheKeyList = new ArrayList<>();//批量查询的所有商品key for (Long skuid : skuIdList) { redisCacheKeyList.add(StoreProductCacheKeyService.getStoreProductPrice(skuid)); } return redisCacheKeyList; }
标签:缓存,return,String,cache,redis,private,key,public,store From: https://www.cnblogs.com/niun/p/17407914.html