库存记录 存入redis

This commit is contained in:
wangw 2024-03-11 18:32:56 +08:00
parent b590a3260a
commit ce3ef6b8d3
3 changed files with 50 additions and 1 deletions

View File

@ -60,4 +60,8 @@ public interface CacheKey {
* 数据字典
*/
String DICT_NAME = "dict:name:";
/**
* 库存 PRODUCT:shopid:skuid
*/
String PRODUCT_SKU = "PRODUCT:";
}

View File

@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
@ -693,6 +694,10 @@ public class RedisUtils {
}
}
public void incrBy(String key, double delta) {
redisTemplate.opsForValue().increment(key, delta);
}
/**
* @param prefix 前缀
* @param ids id
@ -709,4 +714,32 @@ public class RedisUtils {
log.debug("缓存删除数量:" + count + "");
log.debug("--------------------------------------------");
}
public void delByIntKey(String prefix, Set<Integer> ids) {
Set<Object> keys = new HashSet<>();
for (Integer id : ids) {
keys.addAll(redisTemplate.keys(new StringBuffer(prefix).append(id).toString()));
}
long count = redisTemplate.delete(keys);
// 此处提示可自行删除
log.debug("--------------------------------------------");
log.debug("成功删除缓存:" + keys.toString());
log.debug("缓存删除数量:" + count + "");
log.debug("--------------------------------------------");
}
/**
* redis 库存操作
* @param type 1为修改库存 2为移除库存
* @param map k:
*/
public void redisUp(Integer type,String shopId,Map<Integer,Double> map) {
String redisKey= CacheKey.PRODUCT_SKU+shopId+ ":";
if(type==1) {
for (Map.Entry<Integer, Double> entry : map.entrySet()) {
incrBy(redisKey+entry.getKey(),entry.getValue());
}
} else if (type == 2) {
delByIntKey(redisKey,map.keySet());
}
}
}

View File

@ -39,6 +39,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
@ -69,6 +70,8 @@ public class TbProductServiceImpl implements TbProductService {
private final TbProductSpecRepository tbProductSpecRepository;
private final TbProductSkuResultRepository tbProductSkuResultRepository;
private final RedisUtils redisUtils;
@Override
public Map<String,Object> queryAll(TbProductQueryCriteria criteria){
@ -261,7 +264,10 @@ public class TbProductServiceImpl implements TbProductService {
sku.setUpdatedAt(Instant.now().toEpochMilli());
skuList.add(sku);
}
tbProductSkuRepository.saveAll(skuList);
List<TbProductSku> tbProductSkus = tbProductSkuRepository.saveAll(skuList);
Map<Integer, Double> idStockMap = tbProductSkus.stream()
.collect(Collectors.toMap(TbProductSku::getId, TbProductSku::getStockNumber));
redisUtils.redisUp(1,save.getShopId(),idStockMap);
}
//保存到sku_result
if ("sku".equals(resources.getTypeEnum())){
@ -325,6 +331,12 @@ public class TbProductServiceImpl implements TbProductService {
public void deleteAll(Integer[] ids) {
List<Integer> list = Arrays.asList(ids);
tbProductRepository.updateByStatus(list);
list.forEach(productId->{
List<TbProductSku> tbProductSkus = tbProductSkuRepository.searchSku(productId.toString());
Map<Integer, Double> idStockMap = tbProductSkus.stream()
.collect(Collectors.toMap(TbProductSku::getId, TbProductSku::getStockNumber));
redisUtils.redisUp(2,productId.toString(),idStockMap);
});
}
@Override