Authored by lijian

缓存优化

@@ -10,8 +10,8 @@ import com.yoho.error.exception.ServiceException; @@ -10,8 +10,8 @@ import com.yoho.error.exception.ServiceException;
10 import org.slf4j.Logger; 10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory; 11 import org.slf4j.LoggerFactory;
12 import org.springframework.stereotype.Controller; 12 import org.springframework.stereotype.Controller;
13 -import org.springframework.web.bind.annotation.RequestBody;  
14 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RequestMapping;
  14 +import org.springframework.web.bind.annotation.RequestParam;
15 import org.springframework.web.bind.annotation.ResponseBody; 15 import org.springframework.web.bind.annotation.ResponseBody;
16 16
17 import javax.annotation.Resource; 17 import javax.annotation.Resource;
@@ -42,14 +42,15 @@ public class FavoriteController { @@ -42,14 +42,15 @@ public class FavoriteController {
42 */ 42 */
43 @RequestMapping("/addFavorite") 43 @RequestMapping("/addFavorite")
44 @ResponseBody 44 @ResponseBody
45 - public ApiResponse addFavorite(@RequestBody FavoriteAddReq favoriteAddReq) throws ServiceException {  
46 - log.info("getBrandActInfoById req is {}", favoriteAddReq);  
47 - if (favoriteAddReq == null) {  
48 - log.warn("getBrandActInfoById req is {}", favoriteAddReq); 45 + public ApiResponse addFavorite(@RequestParam(value = "productSkn") int skn, @RequestParam(value = "uid") int uid) throws ServiceException {
  46 + log.info("getBrandActInfoById req is {},{}", skn,uid);
  47 + if (skn ==0||uid==0) {
  48 + log.warn("getBrandActInfoById req is {},{}", skn, uid);
49 return new ApiResponse(403, "参数异常"); 49 return new ApiResponse(403, "参数异常");
50 } 50 }
51 - favoriteAddReq.setProductSkn(512579932);  
52 - favoriteAddReq.setUid(8041532); 51 + FavoriteAddReq favoriteAddReq=new FavoriteAddReq();
  52 + favoriteAddReq.setProductSkn(skn);
  53 + favoriteAddReq.setUid(uid);
53 ApiResponse apiResponse = favoriteActivityService.addUserFavorite(favoriteAddReq); 54 ApiResponse apiResponse = favoriteActivityService.addUserFavorite(favoriteAddReq);
54 return apiResponse; 55 return apiResponse;
55 } 56 }
@@ -6,6 +6,7 @@ package com.yoho.activity.service.impl; @@ -6,6 +6,7 @@ package com.yoho.activity.service.impl;
6 import com.alibaba.fastjson.JSON; 6 import com.alibaba.fastjson.JSON;
7 import com.yoho.activity.common.ApiResponse; 7 import com.yoho.activity.common.ApiResponse;
8 import com.yoho.activity.common.bo.ActNewBrandBo; 8 import com.yoho.activity.common.bo.ActNewBrandBo;
  9 +import com.yoho.activity.common.redis.CacheKeyHelper;
9 import com.yoho.activity.common.utils.BeanTool; 10 import com.yoho.activity.common.utils.BeanTool;
10 import com.yoho.activity.common.utils.DateUtils; 11 import com.yoho.activity.common.utils.DateUtils;
11 import com.yoho.activity.dal.ActNewBrandMapper; 12 import com.yoho.activity.dal.ActNewBrandMapper;
@@ -14,6 +15,7 @@ import com.yoho.activity.dal.model.ActNewBrand; @@ -14,6 +15,7 @@ import com.yoho.activity.dal.model.ActNewBrand;
14 import com.yoho.activity.dal.model.ActNewBrandResult; 15 import com.yoho.activity.dal.model.ActNewBrandResult;
15 import com.yoho.activity.service.IBrandActivityService; 16 import com.yoho.activity.service.IBrandActivityService;
16 import com.yoho.activity.service.ICouponActivityService; 17 import com.yoho.activity.service.ICouponActivityService;
  18 +import com.yoho.core.redis.YHValueOperations;
17 import com.yoho.core.rest.client.ServiceCaller; 19 import com.yoho.core.rest.client.ServiceCaller;
18 import com.yoho.error.exception.ServiceException; 20 import com.yoho.error.exception.ServiceException;
19 import com.yoho.service.model.request.YohoCoinCostReqBO; 21 import com.yoho.service.model.request.YohoCoinCostReqBO;
@@ -27,6 +29,7 @@ import org.springframework.stereotype.Service; @@ -27,6 +29,7 @@ import org.springframework.stereotype.Service;
27 29
28 import javax.annotation.Resource; 30 import javax.annotation.Resource;
29 import java.util.List; 31 import java.util.List;
  32 +import java.util.concurrent.TimeUnit;
30 33
31 /** 34 /**
32 * 描述:品牌抽奖活动 35 * 描述:品牌抽奖活动
@@ -51,6 +54,14 @@ public class BrandActivityServiceImpl implements IBrandActivityService { @@ -51,6 +54,14 @@ public class BrandActivityServiceImpl implements IBrandActivityService {
51 @Resource 54 @Resource
52 private ServiceCaller serviceCaller; 55 private ServiceCaller serviceCaller;
53 56
  57 + @Resource(name = "yhValueOperations")
  58 + private YHValueOperations<String, String> yhValueOperations;
  59 +
  60 + private static String BRAND_ACTIVITY_DRAW="yh:activity:brand:drawlist";
  61 +
  62 + private static String BRAND_ACTIVITY_DRAW_DETAIL="yh:activity:brand:drawdetail:";
  63 +
  64 +
54 /** 65 /**
55 * 根据当前时间查询品牌活动奖项列表 66 * 根据当前时间查询品牌活动奖项列表
56 * 67 *
@@ -61,15 +72,41 @@ public class BrandActivityServiceImpl implements IBrandActivityService { @@ -61,15 +72,41 @@ public class BrandActivityServiceImpl implements IBrandActivityService {
61 public List<ActNewBrandBo> getBrandActList() throws ServiceException { 72 public List<ActNewBrandBo> getBrandActList() throws ServiceException {
62 //获取当前时间 73 //获取当前时间
63 String dt = DateUtils.getToday(DateUtils.DAY_FOMARTPATTER); 74 String dt = DateUtils.getToday(DateUtils.DAY_FOMARTPATTER);
64 - List<ActNewBrand> actNewBrandList = actNewBrandMapper.selectListByTime(dt);  
65 - logger.info("getBrandActList db res={}", JSON.toJSONString(actNewBrandList)); 75 + List<ActNewBrand> actNewBrandList = null;
  76 + try {
  77 + actNewBrandList= CacheKeyHelper.string2Value(yhValueOperations.get(BRAND_ACTIVITY_DRAW),List.class);
  78 + }catch (Exception e){
  79 + logger.warn("getBrandActList cache error={}", e);
  80 + }
  81 + if(actNewBrandList==null){
  82 + actNewBrandList = actNewBrandMapper.selectListByTime(dt);
  83 + try {
  84 + yhValueOperations.set(BRAND_ACTIVITY_DRAW, CacheKeyHelper.value2String(actNewBrandList),1, TimeUnit.MINUTES);
  85 + }catch (Exception e){
  86 + logger.warn("getBrandActList cache error={}", e.getMessage());
  87 + }
  88 + }
  89 + logger.info("getBrandActList res={}", JSON.toJSONString(actNewBrandList));
66 List<ActNewBrandBo> actNewBrandBoList = BeanTool.copyList(actNewBrandList, ActNewBrandBo.class); 90 List<ActNewBrandBo> actNewBrandBoList = BeanTool.copyList(actNewBrandList, ActNewBrandBo.class);
67 return actNewBrandBoList; 91 return actNewBrandBoList;
68 } 92 }
69 93
70 @Override 94 @Override
71 public ActNewBrandBo getBrandActInfoById(Integer id) throws ServiceException { 95 public ActNewBrandBo getBrandActInfoById(Integer id) throws ServiceException {
72 - ActNewBrand actNewBrand = actNewBrandMapper.selectByPrimaryKey(id); 96 + ActNewBrand actNewBrand = null;
  97 + try {
  98 + actNewBrand= CacheKeyHelper.string2Value(yhValueOperations.get(BRAND_ACTIVITY_DRAW_DETAIL),ActNewBrand.class);
  99 + }catch (Exception e){
  100 + logger.warn("getBrandActInfoById cache error={}", e);
  101 + }
  102 + if(actNewBrand==null){
  103 + actNewBrand = actNewBrandMapper.selectByPrimaryKey(id);
  104 + try {
  105 + yhValueOperations.set(BRAND_ACTIVITY_DRAW_DETAIL, CacheKeyHelper.value2String(actNewBrand),1, TimeUnit.MINUTES);
  106 + }catch (Exception e){
  107 + logger.warn("getBrandActList cache error={}", e.getMessage());
  108 + }
  109 + }
73 ActNewBrandBo actNewBrandBo = BeanTool.copyObject(actNewBrand, ActNewBrandBo.class); 110 ActNewBrandBo actNewBrandBo = BeanTool.copyObject(actNewBrand, ActNewBrandBo.class);
74 return actNewBrandBo; 111 return actNewBrandBo;
75 } 112 }
@@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
16 <script> 16 <script>
17 var aArr = new Array();//每个盒子的点击状态 17 var aArr = new Array();//每个盒子的点击状态
18 var boxNumLimit = 9;//箱子个数 18 var boxNumLimit = 9;//箱子个数
  19 + var uid;
19 $( 20 $(
20 function () { 21 function () {
21 22
@@ -24,10 +25,10 @@ @@ -24,10 +25,10 @@
24 $.getJSON(dataRequestUrl+"?callback=?", 25 $.getJSON(dataRequestUrl+"?callback=?",
25 function(json) { 26 function(json) {
26 if (json.code === 200) { 27 if (json.code === 200) {
27 - alert('登录成功!!!!ID:' + json.data); 28 + uid=json.data;
28 } else { 29 } else {
29 - alert("请先登录!!!");  
30 - 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"}}`; 30 + var url ='https://m.yohobuy.com/signin.html?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":'
  31 + +'/activity/brand/index.html'+'","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"Y"}}';
31 window.location.href=url; 32 window.location.href=url;
32 } 33 }
33 } 34 }
@@ -46,15 +47,14 @@ @@ -46,15 +47,14 @@
46 var arr = data.data; 47 var arr = data.data;
47 if (aArr[liNnm] === 0) { 48 if (aArr[liNnm] === 0) {
48 aArr[liNnm] = 1; 49 aArr[liNnm] = 1;
49 - opened(liNnm);  
50 if (arr.prize == 1) { 50 if (arr.prize == 1) {
51 var newCover = awaCover(arr.prizeRemark); 51 var newCover = awaCover(arr.prizeRemark);
52 - var res = addPrize(88, arr.id); 52 + var res = addPrize(uid, arr.id);
53 if (res.code == 200) { 53 if (res.code == 200) {
54 $("body").append(newCover); 54 $("body").append(newCover);
55 } else { 55 } else {
56 - }  
57 $("body").append(noAwa()); 56 $("body").append(noAwa());
  57 + }
58 } else { 58 } else {
59 var newCover = noAwa(); 59 var newCover = noAwa();
60 $("body").append(newCover); 60 $("body").append(newCover);
@@ -73,22 +73,6 @@ @@ -73,22 +73,6 @@
73 } 73 }
74 ); 74 );
75 75
76 - //测试用,假数据  
77 - /* var dataRequestUrl="http://new.yohoboys.com/customerevent/default/page";  
78 - var _cid=0;  
79 - $.getJSON(dataRequestUrl+"?cid="+_cid+"&offset=0"+"&callback=?",  
80 - function(json) {  
81 - var arr=JSON.parse(json);  
82 - for(var i=0; i<boxNumLimit; i++){  
83 - aArr[i] = 0;  
84 - var _obj=arr[i];  
85 - $(".a-box-ul").append('<li awa="no"><img src='+_obj.pic+' /></li>');  
86 - $(".awa-list-ul ul").append('<li><span>'+ _obj.title+'</span><span>2016/08/08 16:00</span></li>');  
87 - $(".act-list-ul").append('<a><li>'+ _obj.title+'</li></a>');  
88 - }  
89 - }  
90 - );*/  
91 -  
92 $.ajax({ 76 $.ajax({
93 url: '/activity/brandDraw/getBrandActList', 77 url: '/activity/brandDraw/getBrandActList',
94 type: "get", 78 type: "get",
@@ -113,14 +97,16 @@ @@ -113,14 +97,16 @@
113 97
114 $(function () { 98 $(function () {
115 $(".check-awa-btn").live("click", function () { 99 $(".check-awa-btn").live("click", function () {
  100 + $(".awa-list-ul ul").empty();
116 $.ajax({ 101 $.ajax({
117 - url: '/activity/brandDraw/getBrandPrizeListByUid?uid=' + 2, 102 + url: '/activity/brandDraw/getBrandPrizeListByUid?uid=' + uid,
118 type: "get", 103 type: "get",
119 contentType: "application/json", 104 contentType: "application/json",
120 dataType: 'json', 105 dataType: 'json',
121 success: function (data) { 106 success: function (data) {
122 if (data.code === 200) { 107 if (data.code === 200) {
123 var arr = data.data; 108 var arr = data.data;
  109 + $(".awa-list-ul ul").empty();
124 for (var i = 0; i < arr.length; i++) { 110 for (var i = 0; i < arr.length; i++) {
125 var _obj = arr[i]; 111 var _obj = arr[i];
126 if (_obj != null) { 112 if (_obj != null) {
@@ -128,7 +114,6 @@ @@ -128,7 +114,6 @@
128 if (dtime > 0) { 114 if (dtime > 0) {
129 var strTime = new Date(parseInt(dtime) * 1000).toISOString().slice(0, 16).replace("T", " "); 115 var strTime = new Date(parseInt(dtime) * 1000).toISOString().slice(0, 16).replace("T", " ");
130 } 116 }
131 - $(".awa-list-ul ul").empty();  
132 $(".awa-list-ul ul").append('<li><span>' + _obj.prizeRemark + '</span><span>' + strTime + '</span></li>'); 117 $(".awa-list-ul ul").append('<li><span>' + _obj.prizeRemark + '</span><span>' + strTime + '</span></li>');
133 $(".act-list-ul").append('<a><li>' + _obj.brandInfo + '</li></a>'); 118 $(".act-list-ul").append('<a><li>' + _obj.brandInfo + '</li></a>');
134 } 119 }
@@ -96,8 +96,8 @@ function noAwa() { @@ -96,8 +96,8 @@ function noAwa() {
96 '<div class="dia-bg"></div>' 96 '<div class="dia-bg"></div>'
97 return cHtml; 97 return cHtml;
98 } 98 }
99 -function opened(thisBox){ 99 +function opened(thisBox,imgurl){
100 $(".a-box-ul li:eq("+ thisBox +")").find("img").remove(); 100 $(".a-box-ul li:eq("+ thisBox +")").find("img").remove();
101 - $(".a-box-ul li:eq("+ thisBox +")").append("<img src='images/awa.png' />"); 101 + $(".a-box-ul li:eq("+ thisBox +")").append("<img src="+imgurl+" />");
102 } 102 }
103 103