Update RedisConfig.java

1. 注释写的是 默认两小时, 但是代码写的是 6 小时, 所以应指定哪一个 ? 
2. 当没有出现碰撞时, 固定的 3个 kv 对, size 达到 3 个, 如果方法参数不为空, 则会触发 HashMap 扩容, 影响性能
3 + params.length > (2^2 * 0.75 = 3)
2. 仍然引用的是 apache 的方法, 为了去除歧义, 应使用全类名指定
如果方法返回值包含 null, 则加上 @Nullable 注解, 代码更易读
pull/705/head
Night_mare 2021-12-03 15:31:01 +08:00 committed by GitHub
parent 8e353ca0f7
commit 73c4e88eb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 11 deletions

View File

@ -65,7 +65,7 @@ public class RedisConfig extends CachingConfigurerSupport {
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
configuration = configuration.serializeValuesWith(RedisSerializationContext.
SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofHours(6));
SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofHours(2));
return configuration;
}
@ -97,7 +97,7 @@ public class RedisConfig extends CachingConfigurerSupport {
@Override
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
Map<String,Object> container = new HashMap<>(3);
Map<String,Object> container = new HashMap<>();
Class<?> targetClassClass = target.getClass();
// 类地址
container.put("class",targetClassClass.toGenericString());
@ -203,13 +203,14 @@ class StringRedisSerializer implements RedisSerializer<Object> {
return (bytes == null ? null : new String(bytes, charset));
}
@Override
public byte[] serialize(Object object) {
String string = JSON.toJSONString(object);
if (StringUtils.isBlank(string)) {
return null;
}
string = string.replace("\"", "");
return string.getBytes(charset);
}
@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);
}
}