库存记录 存入redis

This commit is contained in:
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());
}
}
}