商品读取缓存
This commit is contained in:
parent
eda1e841bb
commit
0c1d3236e8
@ -8,6 +8,9 @@ import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
|||||||
import cn.lili.modules.goods.entity.vos.GoodsVO;
|
import cn.lili.modules.goods.entity.vos.GoodsVO;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import org.springframework.cache.annotation.CacheConfig;
|
||||||
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -17,6 +20,7 @@ import java.util.List;
|
|||||||
* @author pikachu
|
* @author pikachu
|
||||||
* @date 2020-02-23 16:18:56
|
* @date 2020-02-23 16:18:56
|
||||||
*/
|
*/
|
||||||
|
@CacheConfig(cacheNames = "{goods}")
|
||||||
public interface GoodsService extends IService<Goods> {
|
public interface GoodsService extends IService<Goods> {
|
||||||
|
|
||||||
|
|
||||||
@ -48,6 +52,7 @@ public interface GoodsService extends IService<Goods> {
|
|||||||
* @param goodsOperationDTO 商品查询条件
|
* @param goodsOperationDTO 商品查询条件
|
||||||
* @param goodsId 商品ID
|
* @param goodsId 商品ID
|
||||||
*/
|
*/
|
||||||
|
@CacheEvict(key = "#goodsId")
|
||||||
void editGoods(GoodsOperationDTO goodsOperationDTO, String goodsId);
|
void editGoods(GoodsOperationDTO goodsOperationDTO, String goodsId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,6 +61,7 @@ public interface GoodsService extends IService<Goods> {
|
|||||||
* @param goodsId 商品id
|
* @param goodsId 商品id
|
||||||
* @return 商品VO
|
* @return 商品VO
|
||||||
*/
|
*/
|
||||||
|
@Cacheable(key = "#goodsId")
|
||||||
GoodsVO getGoodsVO(String goodsId);
|
GoodsVO getGoodsVO(String goodsId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -179,7 +179,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GoodsSku getGoodsSkuByIdFromCache(String id) {
|
public GoodsSku getGoodsSkuByIdFromCache(String id) {
|
||||||
|
//获取缓存中的sku
|
||||||
GoodsSku goodsSku = cache.get(GoodsSkuService.getCacheKeys(id));
|
GoodsSku goodsSku = cache.get(GoodsSkuService.getCacheKeys(id));
|
||||||
|
//如果缓存中没有信息,则查询数据库,然后写入缓存
|
||||||
if (goodsSku == null) {
|
if (goodsSku == null) {
|
||||||
goodsSku = this.getById(id);
|
goodsSku = this.getById(id);
|
||||||
if (goodsSku == null) {
|
if (goodsSku == null) {
|
||||||
@ -187,6 +189,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> i
|
|||||||
}
|
}
|
||||||
cache.put(GoodsSkuService.getCacheKeys(id), goodsSku);
|
cache.put(GoodsSkuService.getCacheKeys(id), goodsSku);
|
||||||
}
|
}
|
||||||
|
//获取商品库存
|
||||||
String quantity = stringRedisTemplate.opsForValue().get(GoodsSkuService.getStockCacheKey(id));
|
String quantity = stringRedisTemplate.opsForValue().get(GoodsSkuService.getStockCacheKey(id));
|
||||||
if (quantity != null) {
|
if (quantity != null) {
|
||||||
if (goodsSku.getQuantity().equals(Convert.toInt(quantity))) {
|
if (goodsSku.getQuantity().equals(Convert.toInt(quantity))) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user