Authored by TANLING

add cache

@@ -13,6 +13,11 @@ public class KeyBuilder { @@ -13,6 +13,11 @@ public class KeyBuilder {
13 return RedisKeyBuilder.newInstance().appendFixed("ufo:promotion:couponLimitProduct:id:").appendVar(couponId); 13 return RedisKeyBuilder.newInstance().appendFixed("ufo:promotion:couponLimitProduct:id:").appendVar(couponId);
14 } 14 }
15 15
  16 + public static RedisKeyBuilder buildCouponLimitProductValueCacheKey(Integer couponId) {
  17 + return RedisKeyBuilder.newInstance().appendFixed("ufo:promotion:couponLimitProductValue:id:").appendVar(couponId);
  18 + }
  19 +
  20 +
16 public static RedisKeyBuilder buildCouponTypeCacheKey(int id) { 21 public static RedisKeyBuilder buildCouponTypeCacheKey(int id) {
17 return RedisKeyBuilder.newInstance().appendFixed("ufo:promotion:couponType:id:").appendVar(id); 22 return RedisKeyBuilder.newInstance().appendFixed("ufo:promotion:couponType:id:").appendVar(id);
18 } 23 }
1 package com.yohoufo.promotion.service; 1 package com.yohoufo.promotion.service;
2 2
  3 +import com.yohobuy.ufo.model.promotion.request.ProductLimitValueBo;
3 import com.yohoufo.dal.promotion.model.Coupon; 4 import com.yohoufo.dal.promotion.model.Coupon;
4 import com.yohoufo.dal.promotion.model.CouponAndType; 5 import com.yohoufo.dal.promotion.model.CouponAndType;
5 import com.yohoufo.dal.promotion.model.CouponType; 6 import com.yohoufo.dal.promotion.model.CouponType;
@@ -37,4 +38,12 @@ public interface ICouponCacheService { @@ -37,4 +38,12 @@ public interface ICouponCacheService {
37 * @return 38 * @return
38 */ 39 */
39 public Map<Integer, List<Integer>> getLimitProductWithCache(List<Integer> productLimitCouponIds); 40 public Map<Integer, List<Integer>> getLimitProductWithCache(List<Integer> productLimitCouponIds);
  41 +
  42 +
  43 + /**
  44 + * 获取couponId对应的productLimitValue
  45 + * @param productLimitCouponIds
  46 + * @return
  47 + */
  48 + public Map<Integer, List<ProductLimitValueBo>> getLimitProductValueWithCache(List<Integer> productLimitCouponIds);
40 } 49 }
@@ -4,6 +4,7 @@ package com.yohoufo.promotion.service.impl; @@ -4,6 +4,7 @@ package com.yohoufo.promotion.service.impl;
4 import com.google.common.collect.Lists; 4 import com.google.common.collect.Lists;
5 import com.google.common.collect.Maps; 5 import com.google.common.collect.Maps;
6 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; 6 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
  7 +import com.yohobuy.ufo.model.promotion.request.ProductLimitValueBo;
7 import com.yohoufo.common.cache.CacheClient; 8 import com.yohoufo.common.cache.CacheClient;
8 import com.yohoufo.dal.promotion.CouponMapper; 9 import com.yohoufo.dal.promotion.CouponMapper;
9 import com.yohoufo.dal.promotion.CouponProductLimitMapper; 10 import com.yohoufo.dal.promotion.CouponProductLimitMapper;
@@ -111,6 +112,77 @@ public class CouponCacheServiceImpl implements ICouponCacheService { @@ -111,6 +112,77 @@ public class CouponCacheServiceImpl implements ICouponCacheService {
111 return coupon; 112 return coupon;
112 } 113 }
113 114
  115 +
  116 + /**
  117 + * 获取couponId对应的productLimitValue
  118 + * @param productLimitCouponIds
  119 + * @return
  120 + */
  121 + public Map<Integer, List<ProductLimitValueBo>> getLimitProductValueWithCache(List<Integer> productLimitCouponIds) {
  122 + Map<Integer, List<ProductLimitValueBo>> result = Maps.newHashMap();
  123 +
  124 + List<Integer> missCouponIdList = Lists.newArrayList();
  125 +
  126 + for (Integer couponId : productLimitCouponIds){
  127 + List<ProductLimitValueBo> productIds = cacheClient.range(KeyBuilder.buildCouponLimitProductValueCacheKey(couponId), ProductLimitValueBo.class,0, -1);
  128 +
  129 + if (CollectionUtils.isEmpty(productIds)){
  130 + missCouponIdList.add(couponId);
  131 + }else{
  132 + result.put(couponId, productIds);
  133 + }
  134 + }
  135 +
  136 + if (CollectionUtils.isEmpty(missCouponIdList)){
  137 + return result;
  138 + }
  139 +
  140 + Map<Integer, List<ProductLimitValueBo>> resultCache = getAssociatedProductLimitValueMap(missCouponIdList);
  141 + result.putAll(resultCache);
  142 +
  143 + for (Integer key : resultCache.keySet()){
  144 + cacheClient.rightPushAll(KeyBuilder.buildCouponLimitProductValueCacheKey(key), resultCache.get(key), ExpiredTime.COUPON_PRODUCT_LIMIT_CACHE_TIME);
  145 + }
  146 +
  147 + return result;
  148 +
  149 + }
  150 +
  151 +
  152 + /**
  153 + * key=couponId, value=(type, productIdlist)
  154 + * @param couponIdList
  155 + * @return
  156 + */
  157 + private Map<Integer, List<ProductLimitValueBo>> getAssociatedProductLimitValueMap(List<Integer> couponIdList){
  158 +
  159 +
  160 + List<CouponProductLimit> couponProductLimitList = couponProductLimitMapper.selectByCouponIds(couponIdList);
  161 +
  162 + Map<Integer, List<CouponProductLimit>> map = couponProductLimitList.stream().collect(Collectors.groupingBy(CouponProductLimit::getCouponId));
  163 +
  164 + // key=couponId, value=
  165 + Map<Integer, List<ProductLimitValueBo>> productLimitValueBoMap = Maps.newHashMap();
  166 +
  167 + for (Integer couponId : map.keySet()){
  168 +
  169 + List<ProductLimitValueBo> productLimitValueBos = Lists.newArrayList();
  170 +
  171 + // key=limitType, value=
  172 + Map<Integer, List<CouponProductLimit>> typeToItem = map.get(couponId).stream().collect(Collectors.groupingBy(CouponProductLimit::getLimitType));
  173 + for (Integer type : typeToItem.keySet()){
  174 + productLimitValueBos.add(ProductLimitValueBo.builder()
  175 + .limitType(type)
  176 + .productIds(typeToItem.get(type).stream().map(CouponProductLimit::getProductId).collect(Collectors.toList())).build());
  177 + }
  178 +
  179 + productLimitValueBoMap.put(couponId, productLimitValueBos);
  180 +
  181 + }
  182 +
  183 + return productLimitValueBoMap;
  184 + }
  185 +
114 /** 186 /**
115 * 获取couponId对应的productIds 187 * 获取couponId对应的productIds
116 * @param productLimitCouponIds 188 * @param productLimitCouponIds
@@ -444,33 +444,18 @@ public class CouponServiceImpl implements ICouponService { @@ -444,33 +444,18 @@ public class CouponServiceImpl implements ICouponService {
444 return UserCouponsListBo.builder().coupons(couponBoList).build(); 444 return UserCouponsListBo.builder().coupons(couponBoList).build();
445 } 445 }
446 446
  447 + /**
  448 + * key=couponId, value=(type, productIdlist)
  449 + * @param couponList
  450 + * @return
  451 + */
447 private Map<Integer, List<ProductLimitValueBo>> getAssociatedProductIdMap(List<Coupon> couponList){ 452 private Map<Integer, List<ProductLimitValueBo>> getAssociatedProductIdMap(List<Coupon> couponList){
448 453
449 -  
450 - List<CouponProductLimit> couponProductLimitList = couponProductLimitMapper.selectByCouponIds(couponList.stream().map(Coupon::getId).collect(Collectors.toList()));  
451 -  
452 - Map<Integer, List<CouponProductLimit>> map = couponProductLimitList.stream().collect(Collectors.groupingBy(CouponProductLimit::getCouponId));  
453 -  
454 - // key=couponId, value=  
455 - Map<Integer, List<ProductLimitValueBo>> productLimitValueBoMap = Maps.newHashMap();  
456 -  
457 - for (Integer couponId : map.keySet()){  
458 -  
459 - List<ProductLimitValueBo> productLimitValueBos = Lists.newArrayList();  
460 -  
461 - // key=limitType, value=  
462 - Map<Integer, List<CouponProductLimit>> typeToItem = map.get(couponId).stream().collect(Collectors.groupingBy(CouponProductLimit::getLimitType));  
463 - for (Integer type : typeToItem.keySet()){  
464 - productLimitValueBos.add(ProductLimitValueBo.builder()  
465 - .limitType(type)  
466 - .productIds(typeToItem.get(type).stream().map(CouponProductLimit::getProductId).collect(Collectors.toList())).build());  
467 - }  
468 -  
469 - productLimitValueBoMap.put(couponId, productLimitValueBos);  
470 - 454 + if (CollectionUtils.isEmpty(couponList)){
  455 + return Maps.newHashMap();
471 } 456 }
472 457
473 - return productLimitValueBoMap; 458 + return couponCacheService.getLimitProductValueWithCache(couponList.stream().map(Coupon::getId).collect(Collectors.toList()));
474 } 459 }
475 460
476 /** 461 /**