版本管理(控制LDBA-APP 零点八零)
字典复用
This commit is contained in:
@@ -19,11 +19,15 @@ import cn.hutool.core.lang.Assert;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.parser.ParserConfig;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import io.lettuce.core.RedisClient;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
@@ -32,8 +36,11 @@ import org.springframework.cache.interceptor.CacheErrorHandler;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||
@@ -54,9 +61,44 @@ import java.util.Map;
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
@ConditionalOnClass(RedisOperations.class)
|
||||
@EnableConfigurationProperties(RedisProperties.class)
|
||||
//@EnableConfigurationProperties(RedisProperties.class)
|
||||
public class RedisConfig extends CachingConfigurerSupport {
|
||||
|
||||
@Autowired
|
||||
private RedisProperties redisProperties;
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public RedisConnectionFactory redisConnectionFactory() {
|
||||
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());
|
||||
lettuceConnectionFactory.setPassword(redisProperties.getPassword());
|
||||
lettuceConnectionFactory.setDatabase(redisProperties.getDatabase());
|
||||
return lettuceConnectionFactory;
|
||||
}
|
||||
@Bean
|
||||
public RedisConnectionFactory redisConnectionFactory5() {
|
||||
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());
|
||||
lettuceConnectionFactory.setPassword(redisProperties.getPassword());
|
||||
lettuceConnectionFactory.setDatabase(5);
|
||||
return lettuceConnectionFactory;
|
||||
}
|
||||
|
||||
@Bean(name = "redis5Template")
|
||||
public RedisTemplate<Object, Object> redis5Template(@Qualifier("redisConnectionFactory5") RedisConnectionFactory redisConnectionFactory5) {
|
||||
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
||||
//序列化
|
||||
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
|
||||
// value值的序列化采用fastJsonRedisSerializer
|
||||
template.setValueSerializer(fastJsonRedisSerializer);
|
||||
template.setHashValueSerializer(fastJsonRedisSerializer);
|
||||
// key的序列化采用StringRedisSerializer
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setHashKeySerializer(new StringRedisSerializer());
|
||||
template.setConnectionFactory(redisConnectionFactory5);
|
||||
// 配置序列化等信息
|
||||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 redis 数据默认过期时间,默认2小时
|
||||
* 设置@cacheable 序列化方式
|
||||
@@ -73,7 +115,7 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||
@SuppressWarnings("all")
|
||||
@Bean(name = "redisTemplate")
|
||||
@ConditionalOnMissingBean(name = "redisTemplate")
|
||||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||
public RedisTemplate<Object, Object> redisTemplate(@Qualifier("redisConnectionFactory")RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
||||
//序列化
|
||||
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
|
||||
@@ -83,16 +125,11 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||
// fastjson 升级到 1.2.83 后需要指定序列化白名单
|
||||
ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.pojo");
|
||||
ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.service");
|
||||
// 模块内的实体类
|
||||
// ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.mnt.domain");
|
||||
// ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.quartz.domain");
|
||||
// ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.system.domain");
|
||||
// 模块内的 Dto
|
||||
ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.mnt");
|
||||
ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.system");
|
||||
ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.quartz");
|
||||
ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.config");
|
||||
// ParserConfig.getGlobalInstance().addAccept("cn.ysk.cashier.system.service.dto");
|
||||
// key的序列化采用StringRedisSerializer
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setHashKeySerializer(new StringRedisSerializer());
|
||||
@@ -107,17 +144,17 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||
@Override
|
||||
public KeyGenerator keyGenerator() {
|
||||
return (target, method, params) -> {
|
||||
Map<String,Object> container = new HashMap<>(8);
|
||||
Map<String, Object> container = new HashMap<>(8);
|
||||
Class<?> targetClassClass = target.getClass();
|
||||
// 类地址
|
||||
container.put("class",targetClassClass.toGenericString());
|
||||
container.put("class", targetClassClass.toGenericString());
|
||||
// 方法名称
|
||||
container.put("methodName",method.getName());
|
||||
container.put("methodName", method.getName());
|
||||
// 包名称
|
||||
container.put("package",targetClassClass.getPackage());
|
||||
container.put("package", targetClassClass.getPackage());
|
||||
// 参数列表
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
container.put(String.valueOf(i),params[i]);
|
||||
container.put(String.valueOf(i), params[i]);
|
||||
}
|
||||
// 转为JSON字符串
|
||||
String jsonString = JSON.toJSONString(container);
|
||||
@@ -159,10 +196,10 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||
/**
|
||||
* Value 序列化
|
||||
*
|
||||
* @author /
|
||||
* @param <T>
|
||||
* @author /
|
||||
*/
|
||||
class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
|
||||
class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
|
||||
|
||||
private final Class<T> clazz;
|
||||
|
||||
@@ -213,14 +250,14 @@ class StringRedisSerializer implements RedisSerializer<Object> {
|
||||
return (bytes == null ? null : new String(bytes, charset));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable byte[] serialize(Object object) {
|
||||
String string = JSON.toJSONString(object);
|
||||
@Override
|
||||
public @Nullable byte[] serialize(Object object) {
|
||||
String string = JSON.toJSONString(object);
|
||||
|
||||
if (org.apache.commons.lang3.StringUtils.isBlank(string)) {
|
||||
return null;
|
||||
}
|
||||
string = string.replace("\"", "");
|
||||
return string.getBytes(charset);
|
||||
}
|
||||
if (org.apache.commons.lang3.StringUtils.isBlank(string)) {
|
||||
return null;
|
||||
}
|
||||
string = string.replace("\"", "");
|
||||
return string.getBytes(charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,11 +19,14 @@ import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
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.core.Cursor;
|
||||
import org.springframework.data.redis.core.RedisConnectionUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ScanOptions;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -38,14 +41,22 @@ import java.util.concurrent.TimeUnit;
|
||||
public class RedisUtils {
|
||||
private static final Logger log = LoggerFactory.getLogger(RedisUtils.class);
|
||||
private RedisTemplate<Object, Object> redisTemplate;
|
||||
private RedisTemplate<Object, Object> redisTemplate5;
|
||||
|
||||
@Value("${jwt.online-key}")
|
||||
private String onlineKey;
|
||||
|
||||
public RedisUtils(RedisTemplate<Object, Object> redisTemplate) {
|
||||
public RedisUtils(RedisTemplate<Object, Object> redisTemplate,@Qualifier("redis5Template")RedisTemplate<Object, Object> redisTemplate5) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
this.redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
||||
this.redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
this.redisTemplate.setStringSerializer(new StringRedisSerializer());
|
||||
|
||||
|
||||
this.redisTemplate5 = redisTemplate5;
|
||||
this.redisTemplate5.setHashKeySerializer(new StringRedisSerializer());
|
||||
this.redisTemplate5.setKeySerializer(new StringRedisSerializer());
|
||||
this.redisTemplate5.setStringSerializer(new StringRedisSerializer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,7 +229,7 @@ public class RedisUtils {
|
||||
public List<Object> multiGet(List<String> keys) {
|
||||
List list = redisTemplate.opsForValue().multiGet(Sets.newHashSet(keys));
|
||||
List resultList = Lists.newArrayList();
|
||||
Optional.ofNullable(list).ifPresent(e-> list.forEach(ele-> Optional.ofNullable(ele).ifPresent(resultList::add)));
|
||||
Optional.ofNullable(list).ifPresent(e -> list.forEach(ele -> Optional.ofNullable(ele).ifPresent(resultList::add)));
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@@ -717,6 +728,7 @@ public class RedisUtils {
|
||||
|
||||
/**
|
||||
* 根据 prefix+id 删除key 不做模糊匹配
|
||||
*
|
||||
* @param prefix 前缀
|
||||
* @param ids id
|
||||
*/
|
||||
@@ -732,19 +744,74 @@ public class RedisUtils {
|
||||
log.info("缓存删除数量:" + count + "个");
|
||||
log.info("--------------------------------------------");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
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());
|
||||
incrBy(redisKey + entry.getKey(), entry.getValue());
|
||||
}
|
||||
} else if (type == 2) {
|
||||
delByIntKey(redisKey,map.keySet());
|
||||
delByIntKey(redisKey, map.keySet());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ================================================================
|
||||
/****************************** redis 5库 *************************/
|
||||
|
||||
/**
|
||||
* 普通缓存放入
|
||||
* redis5库 (零点八零app库)
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return true成功 false失败
|
||||
*/
|
||||
public boolean set5(String key, Object value) {
|
||||
try {
|
||||
redisTemplate5.opsForValue().set(key, value);
|
||||
// redisTemplate.opsForValue().set(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* redis5库 (零点八零app库)
|
||||
*
|
||||
* @param key 可以传一个值 或多个
|
||||
*/
|
||||
public void del5(String... keys) {
|
||||
if (keys != null && keys.length > 0) {
|
||||
if (keys.length == 1) {
|
||||
boolean result = redisTemplate5.delete(keys[0]);
|
||||
log.debug("--------------------------------------------");
|
||||
log.debug(new StringBuilder("删除缓存:").append(keys[0]).append(",结果:").append(result).toString());
|
||||
log.debug("--------------------------------------------");
|
||||
} else {
|
||||
Set<Object> keySet = new HashSet<>();
|
||||
for (String key : keys) {
|
||||
keySet.addAll(redisTemplate5.keys(key));
|
||||
}
|
||||
long count = redisTemplate5.delete(keySet);
|
||||
log.debug("--------------------------------------------");
|
||||
log.debug("成功删除缓存:" + keySet.toString());
|
||||
log.debug("缓存删除数量:" + count + "个");
|
||||
log.debug("--------------------------------------------");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user