Authored by chenchao

add cache

... ... @@ -56,7 +56,16 @@ public class CacheKeyBuilder {
BUYER_ORDER_NUMS("ufo:order:buyer:orderNums:", "uid:{}"),
BUYER_CANCEL_PENALTY("ufo:order:buyer:cancel:penalty:config", "");
BUYER_CANCEL_PENALTY("ufo:order:buyer:cancel:penalty:config", ""),
ENTRY_THRESHOLD("ufo:order:seller:entry:threshold", ""),
SELLER_BASE_FUNC_CONFIG("ufo:order:seller:base:func:Config", ""),
SELLER_LEVEL_FUNC_CONFIG("ufo:order:seller:stageLevel:func:Config", "");
private String fix;
private String placeHolder;
... ... @@ -130,4 +139,17 @@ public class CacheKeyBuilder {
public static RedisKeyBuilder buyerCancelPenaltyConfigKey(){
return KeyTemp.BUYER_CANCEL_PENALTY.builderKeyOnlyFixed();
}
public static RedisKeyBuilder entryThresholdConfigKey(){
return KeyTemp.ENTRY_THRESHOLD.builderKeyOnlyFixed();
}
public static RedisKeyBuilder sellerBaseFuncConfigKey(){
return KeyTemp.SELLER_BASE_FUNC_CONFIG.builderKeyOnlyFixed();
}
public static RedisKeyBuilder sellerFuncLevelConfigKey(){
return KeyTemp.SELLER_LEVEL_FUNC_CONFIG.builderKeyOnlyFixed();
}
}
... ...
... ... @@ -82,13 +82,21 @@ public class MetaConfigService {
}
public Map<EntrySellerType,EntryThreshold> getEntryThreshold(){
RedisKeyBuilder rkb = CacheKeyBuilder.entryThresholdConfigKey();
String configVal = cacheClient.get(rkb, String.class);
Map<EntrySellerType,EntryThreshold> map = new HashMap<>(2);
String key = MetaConfigKey.SELLER_ENTER_THRESHOLD;
MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
logger.info("in getEntryThreshold from DB {}", metaConfig);
String configVal = null;
if (StringUtils.isBlank(configVal)){
String key = MetaConfigKey.SELLER_ENTER_THRESHOLD;
MetaConfig metaConfig = metaConfigMapper.selectByCode(key);
logger.info("in getEntryThreshold from DB {}", metaConfig);
configVal=metaConfig.getValue();
if (StringUtils.isNotBlank(configVal)){
cacheClient.setEx(rkb, configVal, 300);
}
}
try {
map = JSONObject.parseObject(configVal=metaConfig.getValue(), new TypeReference<Map<EntrySellerType,EntryThreshold>>(){});
map = JSONObject.parseObject(configVal, new TypeReference<Map<EntrySellerType,EntryThreshold>>(){});
}catch (Exception ex){
map = buildDefaultEntryThreshold();
logger.warn("in getEntryThreshold parse value fail,configVal {} use default {}", configVal, map, ex);
... ...
package com.yohoufo.order.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yohobuy.ufo.model.enums.StoredSellerStatusEnum;
import com.yohobuy.ufo.model.order.bo.SellerBo;
import com.yohobuy.ufo.model.order.bo.SellerFuncBo;
... ... @@ -8,6 +10,7 @@ import com.yohobuy.ufo.model.order.bo.SellerLevelFuncBo;
import com.yohobuy.ufo.model.order.common.EntrySellerType;
import com.yohobuy.ufo.model.order.common.SuperEnterStageLevel;
import com.yohobuy.ufo.model.order.resp.EntryThreshold;
import com.yohoufo.common.cache.CacheClient;
import com.yohoufo.dal.order.SellerFuncMapper;
import com.yohoufo.dal.order.SellerLevelFuncMapper;
import com.yohoufo.dal.order.model.SellerFunc;
... ... @@ -15,6 +18,7 @@ import com.yohoufo.dal.order.model.SellerLevelFunc;
import com.yohoufo.dal.order.model.StoredSeller;
import com.yohoufo.order.constants.SellerConfig;
import com.yohoufo.order.service.MerchantOrderPaymentService;
import com.yohoufo.order.service.cache.CacheKeyBuilder;
import com.yohoufo.order.utils.LoggerUtils;
import com.yohoufo.order.utils.SellerHelper;
import org.apache.commons.collections.CollectionUtils;
... ... @@ -48,6 +52,9 @@ public class SellerFuncService {
@Autowired
private MetaConfigService metaConfigService;
@Autowired
private CacheClient cacheClient;
public List<Integer> getFuncIdList(String lflStr){
List<Integer> funcIds = null;
if (StringUtils.isNotBlank(lflStr)){
... ... @@ -75,6 +82,8 @@ public class SellerFuncService {
return slfMap;
}
private SellerLevelFuncBo getDefaultSLFB(){
SellerLevelFuncBo defaultSLFB = new SellerLevelFuncBo();
defaultSLFB.setLevel(SellerConfig.ENTER_TYPE_DEFAULT_LEVEL);
... ... @@ -168,37 +177,61 @@ public class SellerFuncService {
return slfb;
}
private Map<Integer,SellerFuncBo> buildCodeSellerFuncMap(){
List<SellerFunc> sellerFuncs = sellerFuncMapper.selectAll();
Map<Integer,SellerFuncBo> codeSellerFuncMap = sellerFuncs.parallelStream()
.map(sf->{
SellerFuncBo sfb = new SellerFuncBo();
sfb.setId(sf.getId());
sfb.setCode(sf.getCode());
sfb.setCnName(sf.getCnName());
sfb.setEnName(sf.getEnName());
return sfb;
}).collect(Collectors.toMap(SellerFuncBo::getId, Function.identity()));
RedisKeyBuilder rkb = CacheKeyBuilder.sellerBaseFuncConfigKey();
String scsf = cacheClient.get(rkb, String.class);
Map<Integer,SellerFuncBo> codeSellerFuncMap;
if(StringUtils.isBlank(scsf)){
List<SellerFunc> sellerFuncs = sellerFuncMapper.selectAll();
codeSellerFuncMap = sellerFuncs.parallelStream()
.map(sf->{
SellerFuncBo sfb = new SellerFuncBo();
sfb.setId(sf.getId());
sfb.setCode(sf.getCode());
sfb.setCnName(sf.getCnName());
sfb.setEnName(sf.getEnName());
return sfb;
}).collect(Collectors.toMap(SellerFuncBo::getId, Function.identity()));
scsf = JSONObject.toJSONString(codeSellerFuncMap);
logger.info("in buildCodeSellerFuncMap fetch from DB {}", scsf);
cacheClient.setEx(rkb, scsf, 300);
}else{
logger.info("in buildCodeSellerFuncMap hit cache {}", scsf);
codeSellerFuncMap = JSONObject.parseObject(scsf, new TypeReference<Map<Integer,SellerFuncBo>>(){});
}
return codeSellerFuncMap;
}
public Map<Integer, Map<Integer,SellerLevelFuncBo>> getETLVLELFMap(){
List<SellerLevelFunc> slfList = sellerLevelFuncMapper.selectAll();
if (CollectionUtils.isEmpty(slfList)){
return Collections.EMPTY_MAP;
}
Map<Integer,SellerFuncBo> codeSellerFuncMap = buildCodeSellerFuncMap();
RedisKeyBuilder rkb = CacheKeyBuilder.sellerFuncLevelConfigKey();
String sslf = cacheClient.get(rkb, String.class);
Map<Integer, Map<Integer, SellerLevelFuncBo>> ETLVLELFMap = new HashMap<>();
if (StringUtils.isNotBlank(sslf)){
logger.info("in getETLVLELFMap fetch SellerLevelFunc list from cache => {}", sslf);
ETLVLELFMap = JSONObject.parseObject(sslf, new TypeReference<Map<Integer, Map<Integer, SellerLevelFuncBo>>>(){});
}else {
List<SellerLevelFunc> slfList = sellerLevelFuncMapper.selectAll();
if (CollectionUtils.isEmpty(slfList)) {
return Collections.EMPTY_MAP;
}
Map<Integer, SellerFuncBo> codeSellerFuncMap = buildCodeSellerFuncMap();
Map<Integer, Map<Integer,SellerLevelFuncBo>> ETLVLELFMap = new HashMap<>();
for(SellerLevelFunc slf: slfList){
Integer enterType;
if (!ETLVLELFMap.containsKey(enterType=slf.getEnterType())){
ETLVLELFMap.put(enterType, new HashMap<Integer,SellerLevelFuncBo>());
for (SellerLevelFunc slf : slfList) {
Integer enterType;
if (!ETLVLELFMap.containsKey(enterType = slf.getEnterType())) {
ETLVLELFMap.put(enterType, new HashMap<Integer, SellerLevelFuncBo>());
}
SellerLevelFuncBo slfb = do2bo(slf, codeSellerFuncMap);
ETLVLELFMap.get(enterType).put(slf.getLevel(), slfb);
}
SellerLevelFuncBo slfb = do2bo(slf, codeSellerFuncMap);
ETLVLELFMap.get(enterType).put(slf.getLevel(), slfb);
sslf = JSONObject.toJSONString(ETLVLELFMap);
logger.info("in getETLVLELFMap fetch SellerLevelFunc list from DB => {}", sslf);
cacheClient.setEx(rkb, sslf, 300);
}
return ETLVLELFMap;
}
... ...