Authored by chenjian

订单分享领券活动缓存

... ... @@ -19,7 +19,7 @@ public class OrderShareActivityConvert {
return bo;
}
public static OrderShareBO db2boResponse(OrderShareActivity db){
public static OrderShareBO bo2boResponse(OrderShareActivityBO db){
if (db == null) {
return null;
}
... ...
... ... @@ -3,6 +3,7 @@ package com.yoho.activity.service.impl;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
... ... @@ -10,6 +11,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
... ... @@ -20,10 +22,13 @@ import com.yoho.activity.common.bo.OrderShareCouponVO;
import com.yoho.activity.common.constatns.Constant;
import com.yoho.activity.common.convert.OrderShareActivityConvert;
import com.yoho.activity.common.helper.SendSMSHelper;
import com.yoho.activity.common.redis.CacheKeyHelper;
import com.yoho.activity.common.utils.DateUtils;
import com.yoho.activity.common.utils.RandomUtil;
import com.yoho.activity.service.ICouponActivityService;
import com.yoho.activity.service.IOrderShareActivityService;
import com.yoho.core.redis.YHRedisTemplate;
import com.yoho.core.redis.YHValueOperations;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.coupon.dal.IOrderShareActivityDAO;
import com.yoho.coupon.dal.IOrderShareCouponDAO;
... ... @@ -59,6 +64,12 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
@Resource
ServiceCaller serviceCaller;
@Autowired
YHRedisTemplate<String, String> yHRedisTemplate;
@Autowired
YHValueOperations<String, String> yhValueOperations;
@Value("${orderShare.maxNum}")
private String maxshare;// MAX_SHARE_NUM;
... ... @@ -76,13 +87,30 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
@Resource
SendSMSHelper sendSMSHelper;
//订单分享活动缓存KEY(当前只有一个)
public static final String ORDER_SHARE_ACTIVITY_CACHEKEY = "yh:activity:ordershare:activitykey";
@Override
public OrderShareActivityBO getActivityInfoById() {
OrderShareActivityBO activityBO = CacheKeyHelper.string2Value(yhValueOperations.get(ORDER_SHARE_ACTIVITY_CACHEKEY), OrderShareActivityBO.class);
if(activityBO != null) {
log.info("obtain ordershare activity from cache, activityId: {}, activityName: {}", activityBO.getId(), activityBO.getActivityName());
return activityBO;
}
//缓存不命中,从数据库获取
OrderShareActivity db = orderShareActivityDAO.selectOne();
if (db == null) {
log.error("no ordershare activity exists");
return null;
}
return OrderShareActivityConvert.db2bo(db);
activityBO = OrderShareActivityConvert.db2bo(db);
yhValueOperations.set(ORDER_SHARE_ACTIVITY_CACHEKEY, CacheKeyHelper.value2String(activityBO));
yHRedisTemplate.longExpire(ORDER_SHARE_ACTIVITY_CACHEKEY, 1, TimeUnit.MINUTES);
log.debug("ordershare activity, activityId: {}, activityName: {}", activityBO.getId(), activityBO.getActivityName());
return activityBO;
}
@Override
... ... @@ -91,7 +119,8 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
log.info("getShareInfo params is ordercode={},uid={}", ordercode, uid);
// 1、查询此活动
OrderShareBO bo = new OrderShareBO();
OrderShareActivity activitydb = orderShareActivityDAO.selectOne();
OrderShareActivityBO activitydb = getActivityInfoById();
//OrderShareActivity activitydb = orderShareActivityDAO.selectOne();
if (activitydb == null) {
// 活动不存在
log.warn("there is not activity,params is ordercode={},uid={},flag=2", ordercode, uid);
... ... @@ -173,7 +202,7 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
return bo;
}
}
bo = OrderShareActivityConvert.db2boResponse(activitydb);
bo = OrderShareActivityConvert.bo2boResponse(activitydb);
bo.setFlag(1);
bo.setShareUrl(shareUrl + "?ordercode=" + ordercode + "&uid=" + uid);
log.info("getShareInfo result is {}", bo);
... ... @@ -397,20 +426,6 @@ public class OrderShareActivityServiceImpl implements IOrderShareActivityService
}
/**
* 查询活动信息
*
* @param activityId
* @return
*/
private OrderShareActivity getActvivity(int activityId) {
log.info("query order share acitvity, activityId: {}", activityId);
// ....补充缓存操作
// ......
OrderShareActivity activityInfo = orderShareActivityDAO.selectByPrimaryKey(activityId);
return activityInfo;
}
/**
* 判断活动是否过期
*
* @param activityInfo
... ...