Authored by Lixiaodi

修改缓存可以删除

1 package com.yohoufo.product.controller; 1 package com.yohoufo.product.controller;
2 2
  3 +import java.util.concurrent.ExecutorService;
  4 +import java.util.concurrent.Executors;
  5 +import java.util.concurrent.ThreadPoolExecutor;
  6 +import java.util.concurrent.TimeUnit;
  7 +
3 import org.slf4j.Logger; 8 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory; 9 import org.slf4j.LoggerFactory;
5 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
@@ -13,8 +18,11 @@ import com.yoho.tools.docs.ApiOperation; @@ -13,8 +18,11 @@ import com.yoho.tools.docs.ApiOperation;
13 import com.yohoufo.common.ApiResponse; 18 import com.yohoufo.common.ApiResponse;
14 import com.yohoufo.common.annotation.IgnoreSession; 19 import com.yohoufo.common.annotation.IgnoreSession;
15 import com.yohoufo.common.annotation.IgnoreSignature; 20 import com.yohoufo.common.annotation.IgnoreSignature;
  21 +import com.yohoufo.common.cache.Cachable;
  22 +import com.yohoufo.common.cache.ControllerCacheAop;
16 import com.yohoufo.common.caller.UfoServiceCaller; 23 import com.yohoufo.common.caller.UfoServiceCaller;
17 import com.yohoufo.common.utils.UfoJsonUtil; 24 import com.yohoufo.common.utils.UfoJsonUtil;
  25 +import com.yohoufo.dal.product.model.StoragePrice;
18 import com.yohoufo.product.request.StoragePriceBo; 26 import com.yohoufo.product.request.StoragePriceBo;
19 import com.yohoufo.product.response.ProductDetailResp; 27 import com.yohoufo.product.response.ProductDetailResp;
20 import com.yohoufo.product.response.ProductSeriesTemplateResp; 28 import com.yohoufo.product.response.ProductSeriesTemplateResp;
@@ -38,7 +46,7 @@ public class ProductController { @@ -38,7 +46,7 @@ public class ProductController {
38 @IgnoreSignature 46 @IgnoreSignature
39 @IgnoreSession 47 @IgnoreSession
40 @RequestMapping(params = "method=ufo.product.data") 48 @RequestMapping(params = "method=ufo.product.data")
41 - //@Cachable(expire=600) 49 + @Cachable(expire=600)
42 public ApiResponse queryProductDetailById( 50 public ApiResponse queryProductDetailById(
43 @RequestParam(value = "product_id", required = false) Integer productId) { 51 @RequestParam(value = "product_id", required = false) Integer productId) {
44 52
@@ -78,8 +86,6 @@ public class ProductController { @@ -78,8 +86,6 @@ public class ProductController {
78 86
79 87
80 @ApiOperation(name = "ufo.product.storage.leastprice", desc="sku的最低价") 88 @ApiOperation(name = "ufo.product.storage.leastprice", desc="sku的最低价")
81 - @IgnoreSignature  
82 - @IgnoreSession  
83 @RequestMapping(params = "method=ufo.product.storage.leastprice") 89 @RequestMapping(params = "method=ufo.product.storage.leastprice")
84 //@Cachable(expire=600) 90 //@Cachable(expire=600)
85 public ApiResponse queryStorageLeastprice( 91 public ApiResponse queryStorageLeastprice(
@@ -96,8 +102,6 @@ public class ProductController { @@ -96,8 +102,6 @@ public class ProductController {
96 } 102 }
97 103
98 @ApiOperation(name = "ufo.product.storage.data", desc="sku信息") 104 @ApiOperation(name = "ufo.product.storage.data", desc="sku信息")
99 - @IgnoreSignature  
100 - @IgnoreSession  
101 @RequestMapping(params = "method=ufo.product.storage.data") 105 @RequestMapping(params = "method=ufo.product.storage.data")
102 //@Cachable(expire=600) 106 //@Cachable(expire=600)
103 public ApiResponse queryStorageInfo( 107 public ApiResponse queryStorageInfo(
@@ -113,8 +117,7 @@ public class ProductController { @@ -113,8 +117,7 @@ public class ProductController {
113 return resp; 117 return resp;
114 } 118 }
115 119
116 - @IgnoreSignature  
117 - @IgnoreSession 120 + // 增加库存
118 @RequestMapping(params = "method=ufo.product.createSkup") 121 @RequestMapping(params = "method=ufo.product.createSkup")
119 public ApiResponse createSkup(@RequestBody StoragePriceBo skupBo) { 122 public ApiResponse createSkup(@RequestBody StoragePriceBo skupBo) {
120 try { 123 try {
@@ -127,8 +130,7 @@ public class ProductController { @@ -127,8 +130,7 @@ public class ProductController {
127 } 130 }
128 } 131 }
129 132
130 - @IgnoreSignature  
131 - @IgnoreSession 133 + // 售卖
132 @RequestMapping(params = "method=ufo.product.saleSkup") 134 @RequestMapping(params = "method=ufo.product.saleSkup")
133 public ApiResponse saleSkup( 135 public ApiResponse saleSkup(
134 @RequestParam(value = "product_id", required = false) Integer productId, 136 @RequestParam(value = "product_id", required = false) Integer productId,
@@ -143,13 +145,13 @@ public class ProductController { @@ -143,13 +145,13 @@ public class ProductController {
143 } 145 }
144 } 146 }
145 147
146 - @IgnoreSignature  
147 - @IgnoreSession 148 + // 取消售卖
148 @RequestMapping(params = "method=ufo.product.cancelSaleSkup") 149 @RequestMapping(params = "method=ufo.product.cancelSaleSkup")
149 public ApiResponse cancelSaleSkup( 150 public ApiResponse cancelSaleSkup(
150 @RequestParam(value = "skup", required = false) Integer skup) { 151 @RequestParam(value = "skup", required = false) Integer skup) {
151 try { 152 try {
152 productService.cancelSaleSkup(skup); 153 productService.cancelSaleSkup(skup);
  154 + clearProductCache(skup);
153 return new ApiResponse(200, "取消卖出成功!", Boolean.TRUE); 155 return new ApiResponse(200, "取消卖出成功!", Boolean.TRUE);
154 } catch (Exception e) { 156 } catch (Exception e) {
155 LOG.error("取消卖出SKUP失败!", e); 157 LOG.error("取消卖出SKUP失败!", e);
@@ -158,4 +160,23 @@ public class ProductController { @@ -158,4 +160,23 @@ public class ProductController {
158 } 160 }
159 } 161 }
160 162
  163 + @Autowired
  164 + private ControllerCacheAop cacheAop;
  165 + private ExecutorService executors = Executors.newFixedThreadPool(1);
  166 +
  167 + private void clearProductCache(Integer skup) {
  168 + executors.execute(() -> {
  169 + try {
  170 + StoragePrice sp = productService.getStoragePriceBySkup(skup);
  171 + if (sp != null) {
  172 + Integer storageId = sp.getStorageId();
  173 + cacheAop.clearCache(
  174 + ProductController.class.getMethod("queryStorageInfo", new Class[] { Integer.class }),
  175 + new Object[] { storageId });
  176 + }
  177 + } catch (Exception e) {
  178 + LOG.error("删除商品缓存失败!", e);
  179 + }
  180 + });
  181 + }
161 } 182 }
1 package com.yohoufo.product.service; 1 package com.yohoufo.product.service;
2 2
  3 +import com.yohoufo.dal.product.model.StoragePrice;
3 import com.yohoufo.product.request.StoragePriceBo; 4 import com.yohoufo.product.request.StoragePriceBo;
4 import com.yohoufo.product.response.ProductDetailResp; 5 import com.yohoufo.product.response.ProductDetailResp;
5 6
@@ -15,5 +16,7 @@ public interface ProductService { @@ -15,5 +16,7 @@ public interface ProductService {
15 16
16 void cancelSaleSkup(Integer skup); 17 void cancelSaleSkup(Integer skup);
17 18
  19 + StoragePrice getStoragePriceBySkup(Integer skup);
  20 +
18 21
19 } 22 }
@@ -35,6 +35,11 @@ public class ProductServiceImpl implements ProductService{ @@ -35,6 +35,11 @@ public class ProductServiceImpl implements ProductService{
35 } 35 }
36 36
37 @Override 37 @Override
  38 + public StoragePrice getStoragePriceBySkup(Integer skup) {
  39 + return storagePriceMapper.selectBySkup(skup);
  40 + }
  41 +
  42 + @Override
38 public void createSkup(StoragePriceBo skupBo) { 43 public void createSkup(StoragePriceBo skupBo) {
39 storagePriceMapper.insert(exchangeCreateSkupBo(skupBo)); 44 storagePriceMapper.insert(exchangeCreateSkupBo(skupBo));
40 } 45 }
@@ -160,4 +165,5 @@ public class ProductServiceImpl implements ProductService{ @@ -160,4 +165,5 @@ public class ProductServiceImpl implements ProductService{
160 sp.setStatus(1); 165 sp.setStatus(1);
161 return sp; 166 return sp;
162 } 167 }
  168 +
163 } 169 }