1. 修正了 redis-starter 中 cache 反序列化的问题

2. 增加了 redis-cache 实例
This commit is contained in:
Administrator
2020-11-03 16:02:24 +08:00
parent 101e1a87ce
commit b8b47bf595
11 changed files with 279 additions and 76 deletions

View File

@ -9,82 +9,80 @@ import java.nio.charset.StandardCharsets;
/**
* redis序列化器
*
* @author xsx
* @date 2019/7/5
* @since 1.8
*/
public class JsonRedisSerializer implements RedisSerializer<Object> {
/**
* 序列化
* @param t 对象
* @return 返回字节数组
* @throws SerializationException 序列化异常
*/
@Override
public byte[] serialize(Object t) throws SerializationException {
if (t == null) {
return new byte[0];
}
byte[] bytes;
if (this.isSimpleType(t)) {
bytes = t.toString().getBytes(StandardCharsets.UTF_8);
}else {
try {
bytes = JSONUtil.toJsonStr(t).getBytes(StandardCharsets.UTF_8);
} catch (Exception ex) {
throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
}
}
return bytes;
}
/**
* 序列化
*
* @param t 对象
* @return 返回字节数组
* @throws SerializationException 序列化异常
*/
@Override
public byte[] serialize(Object t) throws SerializationException {
if (t == null) {
return new byte[0];
}
byte[] bytes;
if (this.isSimpleType(t)) {
bytes = t.toString().getBytes(StandardCharsets.UTF_8);
} else {
try {
bytes = JSONUtil.toJsonStr(t).getBytes(StandardCharsets.UTF_8);
} catch (Exception ex) {
throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
}
}
return bytes;
}
/**
* 反序列化
* @param bytes 字节数组
* @return 返回对象
* @throws SerializationException 序列化异常
*/
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
return this.tryDeserialize(bytes);
}
/**
* 反序列化
*
* @param bytes 字节数组
* @return 返回对象
* @throws SerializationException 序列化异常
*/
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
return this.tryDeserialize(bytes);
}
/**
* 尝试反序列化
* @param bytes 字节数组
* @return 返回对象
* @throws SerializationException 序列化异常
*/
private Object tryDeserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length == 0) {
return null;
}
String str = new String(bytes, StandardCharsets.UTF_8);
if (JSONUtil.isJsonArray(str)) {
return JSONUtil.parseArray(str);
}else if (JSONUtil.isJsonObj(str)) {
return JSONUtil.parseObj(str);
}else {
return str;
}
}
/**
* 尝试反序列化
*
* @param bytes 字节数组
* @return 返回对象
* @throws SerializationException 序列化异常
*/
private Object tryDeserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length == 0) {
return null;
}
String str = new String(bytes, StandardCharsets.UTF_8);
if (JSONUtil.isJsonArray(str)) {
return JSONUtil.parseArray(str);
} else if (JSONUtil.isJsonObj(str)) {
return JSONUtil.parseObj(str);
}
return str;
}
/**
* 是否简单类型
* @param t 实体
* @return 返回布尔值
*/
private boolean isSimpleType(Object t) {
return SimpleType.BYTE.isValue(t) ||
SimpleType.CHARACTER.isValue(t) ||
SimpleType.SHORT.isValue(t) ||
SimpleType.INTEGER.isValue(t) ||
SimpleType.LONG.isValue(t) ||
SimpleType.FLOAT.isValue(t) ||
SimpleType.DOUBLE.isValue(t) ||
SimpleType.BOOLEAN.isValue(t) ||
SimpleType.BIGINTEGER.isValue(t) ||
SimpleType.BIGDECIMAL.isValue(t);
}
/**
* 是否简单类型
*
* @param t 实体
* @return 返回布尔值
*/
private boolean isSimpleType(Object t) {
return SimpleType.BYTE.isValue(t) || SimpleType.CHARACTER.isValue(t) || SimpleType.SHORT.isValue(t)
|| SimpleType.INTEGER.isValue(t) || SimpleType.LONG.isValue(t) || SimpleType.FLOAT.isValue(t)
|| SimpleType.DOUBLE.isValue(t) || SimpleType.BOOLEAN.isValue(t) || SimpleType.BIGINTEGER.isValue(t)
|| SimpleType.BIGDECIMAL.isValue(t);
}
}

View File

@ -15,6 +15,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.util.StringUtils;
@ -33,7 +34,6 @@ import com.chinaunicom.mall.ebtp.cloud.redis.starter.config.JsonRedisSerializer;
@ConditionalOnMissingBean(CacheManager.class)
@EnableConfigurationProperties({ CacheProperties.class })
public class RedisCacheConfiguration {
@Bean
RedisCacheManager cacheManager(CacheProperties cacheProperties,
ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration,
@ -41,13 +41,15 @@ public class RedisCacheConfiguration {
RedisConnectionFactory redisConnectionFactory) {
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(determineConfiguration(cacheProperties, redisCacheConfiguration));
// 缓存名称
// 应用指定的缓存名称
List<String> cacheNames = cacheProperties.getCacheNames();
// 初始化缓存名称
if (!cacheNames.isEmpty()) {
builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
}
// 以链模式自定义化builder
redisCacheManagerBuilderCustomizers.orderedStream().forEach(customizer -> customizer.customize(builder));
return new CacheManagerCustomizers(null).customize(builder.build());
}
@ -79,7 +81,7 @@ public class RedisCacheConfiguration {
.defaultCacheConfig();
// 替换默认的序列化工具
config = config.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(new JsonRedisSerializer()));
RedisSerializationContext.SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(getClass().getClassLoader())));
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());// 缓存有效时间
}