Created
November 23, 2021 05:52
-
-
Save scruel/9486ca864530eb7c89b36439b6cc032f to your computer and use it in GitHub Desktop.
Spring redis config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*定义缓存数据 key 生成策略的bean | |
包名+类名+方法名+所有参数 | |
*/ | |
@Bean("wiselyKeyGenerator") | |
@Override | |
public KeyGenerator keyGenerator(){ | |
return new KeyGenerator() { | |
@Override | |
public Object generate(Object target, Method method, Object... params) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(target.getClass().getSimpleName()+":"); | |
sb.append(method.getName()+":"); | |
for (Object obj : params) { | |
sb.append(obj.toString()+":"); | |
} | |
return sb.deleteCharAt(sb.length()-1).toString(); | |
} | |
}; | |
} | |
/** | |
* 生成key的策略【自定义第三种】 | |
* 使用范围:仅适用于选取第一个参数做键的情况 | |
* 由于reposotory上不能直接使用spel表达式作key,故而采用key的生成策略的方式来替换 | |
* | |
* 使用时在注解@Cacheable(value = "admins",keyGenerator = "firstParamKeyGenerator")中指定 | |
* @return | |
*/ | |
@Bean | |
public KeyGenerator KeyGenerator() { | |
return (target, method, params) -> { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(target.getClass().getName()); | |
sb.append(method.getName()); | |
for (Object obj : params) { | |
sb.append(obj.toString()); | |
} | |
return sb.toString(); | |
}; | |
} | |
// @Bean | |
// public KeyGenerator keyGenerator() { | |
// return (target, method, params) -> { | |
// StringBuilder sb = new StringBuilder(); | |
// String[] value = new String[1]; | |
// | |
// Cacheable cacheable = method.getAnnotation(Cacheable.class); | |
// if (cacheable != null) { | |
// value = cacheable.value(); | |
// } | |
// // 如果@CachePut指定了key属性之后,则不会再调用keygenerator的方法。所以一定要在 controller 中以以下方式来注解 | |
// // #root.caches[0].name:当前被调用方法所使用的Cache, 即"user" | |
// // @CachePut(value = "user", key = "#root.caches[0].name + ':' + #user.id") | |
// // 更新缓存 | |
// CachePut cachePut = method.getAnnotation(CachePut.class); | |
// if (cachePut != null) { | |
// value = cachePut.value(); | |
// } | |
// // 删除缓存 | |
// CacheEvict cacheEvict = method.getAnnotation(CacheEvict.class); | |
// if (cacheEvict != null) { | |
// value = cacheEvict.value(); | |
// } | |
// sb.append(value[0]); | |
// for (Object obj : params) { | |
// sb.append(":") | |
// .append(obj.toString()); | |
// } | |
// return sb.toString(); | |
// }; | |
// } | |
@Bean | |
public KeyGenerator keyGenerator() { | |
return new KeyGenerator() { | |
@Override | |
public Object generate(Object target, Method method, Object... params) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(target.getClass().getName()); | |
sb.append(method.getName()); | |
for (Object object : params) { | |
sb.append(object.toString()); | |
} | |
return sb.toString(); | |
} | |
}; | |
} | |
@Bean | |
public KeyGenerator keyGenerator() { | |
return new KeyGenerator() { | |
@Override | |
public Object generate(Object target, Method method, Object... params) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(target.getClass().getName()); | |
sb.append(method.getName()); | |
for (Object obj : params) { | |
sb.append(obj.toString()); | |
} | |
return sb.toString(); | |
} | |
}; | |
} | |
# RedisCacheManager | |
RedisCacheManager cacheManager; | |
RedisCacheConfiguration cacheConfiguration =RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(1))//缓存1天 | |
.disableCachingNullValues(); | |
// 设置一个初始化的缓存空间set集合 | |
Set<String> cacheNames = new HashSet<>(); | |
cacheNames.add("login"); | |
// 对每个缓存空间应用不同的配置 | |
Map<String, RedisCacheConfiguration> configMap = new HashMap<>(); | |
configMap.put("login", cacheConfiguration.entryTtl(Duration.ofSeconds(30))); | |
cacheManager = RedisCacheManager.builder(redisConnectionFactory) // 使用自定义的缓存配置初始化一个cacheManager | |
.initialCacheNames(cacheNames) // 注意这两句的调用顺序,一定要先调用该方法设置初始化的缓存名,再初始化相关的配置 | |
.withInitialCacheConfigurations(configMap) | |
.build(); | |
return cacheManager; | |
@Bean | |
CacheManager cacheManager(RedisConnectionFactory connectionFactory) { | |
//user信息缓存配置 | |
RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(30)).disableCachingNullValues().prefixKeysWith("hope"); | |
//product信息缓存配置 | |
RedisCacheConfiguration productCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)).disableCachingNullValues().prefixKeysWith("test"); | |
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(); | |
redisCacheConfigurationMap.put("hope", userCacheConfiguration); | |
redisCacheConfigurationMap.put("test", productCacheConfiguration); | |
//初始化一个RedisCacheWriter | |
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); | |
//设置CacheManager的值序列化方式为JdkSerializationRedisSerializer,但其实RedisCacheConfiguration默认就是使用StringRedisSerializer序列化key,JdkSerializationRedisSerializer序列化value,所以以下注释代码为默认实现 | |
//ClassLoader loader = this.getClass().getClassLoader(); | |
//JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(loader); | |
//RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jdkSerializer); | |
//RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair); | |
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig(); | |
//设置默认超过期时间是30秒 | |
defaultCacheConfig.entryTtl(Duration.ofSeconds(30)); | |
//初始化RedisCacheManager | |
RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap); | |
return cacheManager; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment