Authored by TANLING

add cache

... ... @@ -13,6 +13,11 @@ public class KeyBuilder {
return RedisKeyBuilder.newInstance().appendFixed("ufo:promotion:couponLimitProduct:id:").appendVar(couponId);
}
public static RedisKeyBuilder buildCouponLimitProductValueCacheKey(Integer couponId) {
return RedisKeyBuilder.newInstance().appendFixed("ufo:promotion:couponLimitProductValue:id:").appendVar(couponId);
}
public static RedisKeyBuilder buildCouponTypeCacheKey(int id) {
return RedisKeyBuilder.newInstance().appendFixed("ufo:promotion:couponType:id:").appendVar(id);
}
... ...
package com.yohoufo.promotion.service;
import com.yohobuy.ufo.model.promotion.request.ProductLimitValueBo;
import com.yohoufo.dal.promotion.model.Coupon;
import com.yohoufo.dal.promotion.model.CouponAndType;
import com.yohoufo.dal.promotion.model.CouponType;
... ... @@ -37,4 +38,12 @@ public interface ICouponCacheService {
* @return
*/
public Map<Integer, List<Integer>> getLimitProductWithCache(List<Integer> productLimitCouponIds);
/**
* 获取couponId对应的productLimitValue
* @param productLimitCouponIds
* @return
*/
public Map<Integer, List<ProductLimitValueBo>> getLimitProductValueWithCache(List<Integer> productLimitCouponIds);
}
... ...
... ... @@ -4,6 +4,7 @@ package com.yohoufo.promotion.service.impl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yohobuy.ufo.model.promotion.request.ProductLimitValueBo;
import com.yohoufo.common.cache.CacheClient;
import com.yohoufo.dal.promotion.CouponMapper;
import com.yohoufo.dal.promotion.CouponProductLimitMapper;
... ... @@ -111,6 +112,77 @@ public class CouponCacheServiceImpl implements ICouponCacheService {
return coupon;
}
/**
* 获取couponId对应的productLimitValue
* @param productLimitCouponIds
* @return
*/
public Map<Integer, List<ProductLimitValueBo>> getLimitProductValueWithCache(List<Integer> productLimitCouponIds) {
Map<Integer, List<ProductLimitValueBo>> result = Maps.newHashMap();
List<Integer> missCouponIdList = Lists.newArrayList();
for (Integer couponId : productLimitCouponIds){
List<ProductLimitValueBo> productIds = cacheClient.range(KeyBuilder.buildCouponLimitProductValueCacheKey(couponId), ProductLimitValueBo.class,0, -1);
if (CollectionUtils.isEmpty(productIds)){
missCouponIdList.add(couponId);
}else{
result.put(couponId, productIds);
}
}
if (CollectionUtils.isEmpty(missCouponIdList)){
return result;
}
Map<Integer, List<ProductLimitValueBo>> resultCache = getAssociatedProductLimitValueMap(missCouponIdList);
result.putAll(resultCache);
for (Integer key : resultCache.keySet()){
cacheClient.rightPushAll(KeyBuilder.buildCouponLimitProductValueCacheKey(key), resultCache.get(key), ExpiredTime.COUPON_PRODUCT_LIMIT_CACHE_TIME);
}
return result;
}
/**
* key=couponId, value=(type, productIdlist)
* @param couponIdList
* @return
*/
private Map<Integer, List<ProductLimitValueBo>> getAssociatedProductLimitValueMap(List<Integer> couponIdList){
List<CouponProductLimit> couponProductLimitList = couponProductLimitMapper.selectByCouponIds(couponIdList);
Map<Integer, List<CouponProductLimit>> map = couponProductLimitList.stream().collect(Collectors.groupingBy(CouponProductLimit::getCouponId));
// key=couponId, value=
Map<Integer, List<ProductLimitValueBo>> productLimitValueBoMap = Maps.newHashMap();
for (Integer couponId : map.keySet()){
List<ProductLimitValueBo> productLimitValueBos = Lists.newArrayList();
// key=limitType, value=
Map<Integer, List<CouponProductLimit>> typeToItem = map.get(couponId).stream().collect(Collectors.groupingBy(CouponProductLimit::getLimitType));
for (Integer type : typeToItem.keySet()){
productLimitValueBos.add(ProductLimitValueBo.builder()
.limitType(type)
.productIds(typeToItem.get(type).stream().map(CouponProductLimit::getProductId).collect(Collectors.toList())).build());
}
productLimitValueBoMap.put(couponId, productLimitValueBos);
}
return productLimitValueBoMap;
}
/**
* 获取couponId对应的productIds
* @param productLimitCouponIds
... ...
... ... @@ -444,33 +444,18 @@ public class CouponServiceImpl implements ICouponService {
return UserCouponsListBo.builder().coupons(couponBoList).build();
}
/**
* key=couponId, value=(type, productIdlist)
* @param couponList
* @return
*/
private Map<Integer, List<ProductLimitValueBo>> getAssociatedProductIdMap(List<Coupon> couponList){
List<CouponProductLimit> couponProductLimitList = couponProductLimitMapper.selectByCouponIds(couponList.stream().map(Coupon::getId).collect(Collectors.toList()));
Map<Integer, List<CouponProductLimit>> map = couponProductLimitList.stream().collect(Collectors.groupingBy(CouponProductLimit::getCouponId));
// key=couponId, value=
Map<Integer, List<ProductLimitValueBo>> productLimitValueBoMap = Maps.newHashMap();
for (Integer couponId : map.keySet()){
List<ProductLimitValueBo> productLimitValueBos = Lists.newArrayList();
// key=limitType, value=
Map<Integer, List<CouponProductLimit>> typeToItem = map.get(couponId).stream().collect(Collectors.groupingBy(CouponProductLimit::getLimitType));
for (Integer type : typeToItem.keySet()){
productLimitValueBos.add(ProductLimitValueBo.builder()
.limitType(type)
.productIds(typeToItem.get(type).stream().map(CouponProductLimit::getProductId).collect(Collectors.toList())).build());
}
productLimitValueBoMap.put(couponId, productLimitValueBos);
if (CollectionUtils.isEmpty(couponList)){
return Maps.newHashMap();
}
return productLimitValueBoMap;
return couponCacheService.getLimitProductValueWithCache(couponList.stream().map(Coupon::getId).collect(Collectors.toList()));
}
/**
... ...