Authored by lijian

缓存优化

... ... @@ -10,8 +10,8 @@ import com.yoho.error.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
... ... @@ -42,14 +42,15 @@ public class FavoriteController {
*/
@RequestMapping("/addFavorite")
@ResponseBody
public ApiResponse addFavorite(@RequestBody FavoriteAddReq favoriteAddReq) throws ServiceException {
log.info("getBrandActInfoById req is {}", favoriteAddReq);
if (favoriteAddReq == null) {
log.warn("getBrandActInfoById req is {}", favoriteAddReq);
public ApiResponse addFavorite(@RequestParam(value = "productSkn") int skn, @RequestParam(value = "uid") int uid) throws ServiceException {
log.info("getBrandActInfoById req is {},{}", skn,uid);
if (skn ==0||uid==0) {
log.warn("getBrandActInfoById req is {},{}", skn, uid);
return new ApiResponse(403, "参数异常");
}
favoriteAddReq.setProductSkn(512579932);
favoriteAddReq.setUid(8041532);
FavoriteAddReq favoriteAddReq=new FavoriteAddReq();
favoriteAddReq.setProductSkn(skn);
favoriteAddReq.setUid(uid);
ApiResponse apiResponse = favoriteActivityService.addUserFavorite(favoriteAddReq);
return apiResponse;
}
... ...
... ... @@ -6,6 +6,7 @@ package com.yoho.activity.service.impl;
import com.alibaba.fastjson.JSON;
import com.yoho.activity.common.ApiResponse;
import com.yoho.activity.common.bo.ActNewBrandBo;
import com.yoho.activity.common.redis.CacheKeyHelper;
import com.yoho.activity.common.utils.BeanTool;
import com.yoho.activity.common.utils.DateUtils;
import com.yoho.activity.dal.ActNewBrandMapper;
... ... @@ -14,6 +15,7 @@ import com.yoho.activity.dal.model.ActNewBrand;
import com.yoho.activity.dal.model.ActNewBrandResult;
import com.yoho.activity.service.IBrandActivityService;
import com.yoho.activity.service.ICouponActivityService;
import com.yoho.core.redis.YHValueOperations;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.error.exception.ServiceException;
import com.yoho.service.model.request.YohoCoinCostReqBO;
... ... @@ -27,6 +29,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 描述:品牌抽奖活动
... ... @@ -51,6 +54,14 @@ public class BrandActivityServiceImpl implements IBrandActivityService {
@Resource
private ServiceCaller serviceCaller;
@Resource(name = "yhValueOperations")
private YHValueOperations<String, String> yhValueOperations;
private static String BRAND_ACTIVITY_DRAW="yh:activity:brand:drawlist";
private static String BRAND_ACTIVITY_DRAW_DETAIL="yh:activity:brand:drawdetail:";
/**
* 根据当前时间查询品牌活动奖项列表
*
... ... @@ -61,15 +72,41 @@ public class BrandActivityServiceImpl implements IBrandActivityService {
public List<ActNewBrandBo> getBrandActList() throws ServiceException {
//获取当前时间
String dt = DateUtils.getToday(DateUtils.DAY_FOMARTPATTER);
List<ActNewBrand> actNewBrandList = actNewBrandMapper.selectListByTime(dt);
logger.info("getBrandActList db res={}", JSON.toJSONString(actNewBrandList));
List<ActNewBrand> actNewBrandList = null;
try {
actNewBrandList= CacheKeyHelper.string2Value(yhValueOperations.get(BRAND_ACTIVITY_DRAW),List.class);
}catch (Exception e){
logger.warn("getBrandActList cache error={}", e);
}
if(actNewBrandList==null){
actNewBrandList = actNewBrandMapper.selectListByTime(dt);
try {
yhValueOperations.set(BRAND_ACTIVITY_DRAW, CacheKeyHelper.value2String(actNewBrandList),1, TimeUnit.MINUTES);
}catch (Exception e){
logger.warn("getBrandActList cache error={}", e.getMessage());
}
}
logger.info("getBrandActList res={}", JSON.toJSONString(actNewBrandList));
List<ActNewBrandBo> actNewBrandBoList = BeanTool.copyList(actNewBrandList, ActNewBrandBo.class);
return actNewBrandBoList;
}
@Override
public ActNewBrandBo getBrandActInfoById(Integer id) throws ServiceException {
ActNewBrand actNewBrand = actNewBrandMapper.selectByPrimaryKey(id);
ActNewBrand actNewBrand = null;
try {
actNewBrand= CacheKeyHelper.string2Value(yhValueOperations.get(BRAND_ACTIVITY_DRAW_DETAIL),ActNewBrand.class);
}catch (Exception e){
logger.warn("getBrandActInfoById cache error={}", e);
}
if(actNewBrand==null){
actNewBrand = actNewBrandMapper.selectByPrimaryKey(id);
try {
yhValueOperations.set(BRAND_ACTIVITY_DRAW_DETAIL, CacheKeyHelper.value2String(actNewBrand),1, TimeUnit.MINUTES);
}catch (Exception e){
logger.warn("getBrandActList cache error={}", e.getMessage());
}
}
ActNewBrandBo actNewBrandBo = BeanTool.copyObject(actNewBrand, ActNewBrandBo.class);
return actNewBrandBo;
}
... ...
... ... @@ -16,6 +16,7 @@
<script>
var aArr = new Array();//每个盒子的点击状态
var boxNumLimit = 9;//箱子个数
var uid;
$(
function () {
... ... @@ -24,14 +25,14 @@
$.getJSON(dataRequestUrl+"?callback=?",
function(json) {
if (json.code === 200) {
alert('登录成功!!!!ID:' + json.data);
uid=json.data;
} else {
alert("请先登录!!!");
var url = `https://m.yohobuy.com/signin.html?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"http:\/\/localhost\/%E6%8A%BD%E5%A5%96%E6%B4%BB%E5%8A%A8v3\/index.html","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"Y"}}`;
var url ='https://m.yohobuy.com/signin.html?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":'
+'/activity/brand/index.html'+'","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"Y"}}';
window.location.href=url;
}
}
);
);
$(".a-box-ul li").live("click", function () {
... ... @@ -46,15 +47,14 @@
var arr = data.data;
if (aArr[liNnm] === 0) {
aArr[liNnm] = 1;
opened(liNnm);
if (arr.prize == 1) {
var newCover = awaCover(arr.prizeRemark);
var res = addPrize(88, arr.id);
var res = addPrize(uid, arr.id);
if (res.code == 200) {
$("body").append(newCover);
} else {
$("body").append(noAwa());
}
$("body").append(noAwa());
} else {
var newCover = noAwa();
$("body").append(newCover);
... ... @@ -73,22 +73,6 @@
}
);
//测试用,假数据
/* var dataRequestUrl="http://new.yohoboys.com/customerevent/default/page";
var _cid=0;
$.getJSON(dataRequestUrl+"?cid="+_cid+"&offset=0"+"&callback=?",
function(json) {
var arr=JSON.parse(json);
for(var i=0; i<boxNumLimit; i++){
aArr[i] = 0;
var _obj=arr[i];
$(".a-box-ul").append('<li awa="no"><img src='+_obj.pic+' /></li>');
$(".awa-list-ul ul").append('<li><span>'+ _obj.title+'</span><span>2016/08/08 16:00</span></li>');
$(".act-list-ul").append('<a><li>'+ _obj.title+'</li></a>');
}
}
);*/
$.ajax({
url: '/activity/brandDraw/getBrandActList',
type: "get",
... ... @@ -113,14 +97,16 @@
$(function () {
$(".check-awa-btn").live("click", function () {
$(".awa-list-ul ul").empty();
$.ajax({
url: '/activity/brandDraw/getBrandPrizeListByUid?uid=' + 2,
url: '/activity/brandDraw/getBrandPrizeListByUid?uid=' + uid,
type: "get",
contentType: "application/json",
dataType: 'json',
success: function (data) {
if (data.code === 200) {
var arr = data.data;
$(".awa-list-ul ul").empty();
for (var i = 0; i < arr.length; i++) {
var _obj = arr[i];
if (_obj != null) {
... ... @@ -128,7 +114,6 @@
if (dtime > 0) {
var strTime = new Date(parseInt(dtime) * 1000).toISOString().slice(0, 16).replace("T", " ");
}
$(".awa-list-ul ul").empty();
$(".awa-list-ul ul").append('<li><span>' + _obj.prizeRemark + '</span><span>' + strTime + '</span></li>');
$(".act-list-ul").append('<a><li>' + _obj.brandInfo + '</li></a>');
}
... ...
... ... @@ -96,8 +96,8 @@ function noAwa() {
'<div class="dia-bg"></div>'
return cHtml;
}
function opened(thisBox){
function opened(thisBox,imgurl){
$(".a-box-ul li:eq("+ thisBox +")").find("img").remove();
$(".a-box-ul li:eq("+ thisBox +")").append("<img src='images/awa.png' />");
$(".a-box-ul li:eq("+ thisBox +")").append("<img src="+imgurl+" />");
}
... ...