[英]How to set redis cache prefix key for non spring boot java application using xml configuration
I would like to set a custom cache key prefix for my application which uses xml configuration for my RedisCacheManager, my goal is, if the cache key is student-detail, the cache key should be test:: student-detail or prod:: student-detail, I have already set usePrefix to true, but I cant find a way to define the actual key value.我想为我的应用程序设置一个自定义缓存键前缀,它为我的 RedisCacheManager 使用 xml 配置,我的目标是,如果缓存键是学生详细信息,则缓存键应该是 test:: student-detail 或 prod:: student -detail,我已经将 usePrefix 设置为 true,但是我找不到定义实际键值的方法。 Here below an extract of my cacheManager configuration.下面是我的 cacheManager 配置的摘录。
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
c:redisOperations-ref="redisTemplate"
c:defaultExpiration=3600
c:usePrefix="true">
</bean>
For the information, I know in spring boot is as simple as setting a property in the application properties as:对于信息,我知道在 spring 启动就像在应用程序属性中设置一个属性一样简单:
spring.cache.redis.key-prefix=some::
spring.cache.redis.use-key-prefix=true
Just to put some context why I precised for non spring boot java application.只是为了说明为什么我要针对非 spring 启动 java 应用程序进行精确定位。
The solution is to create your custom redis cache prefix by implementing RedisCachePrefix.See the code below解决方案是通过实现 RedisCachePrefix 创建您的自定义 redis 缓存前缀。参见下面的代码
package com.cache.custom.utils;
import com.morgan.design.properties.ReloadableProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.cache.RedisCachePrefix;
import org.springframework.data.redis.serializer.StringRedisSerializer;
public class MyRedisCachePrefix implements RedisCachePrefix {
/** The Prefix */
@ReloadableProperty("prefixString")
private String personalPrefixString;
/** The key string redis serializer */
@Autowired
String StringRedisSerializer stringRedisSerializer;
/** The delimiter */
private final String delimiter = "::";
@Override
public byte[] prefix(String cacheName) {
return stringRedisSerializer.serialize(personalPrefixString.concat(":").concat(cacheName).concat(this.delimiter));
}
}
Then on the xml:然后在 xml 上:
<bean id=stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="redisCachePrefix"
class="com.cache.custom.utils.MyRedisCachePrefix"/>
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
c:redisOperations-ref="redisTemplate"
p:defaultExpiration=3600
p:usePrefix="true"
p:cachePrefix-ref="redisCachePrefix"
>
</bean>
NB: personalPrefixString value will be taken from properties files entry of prefixString.注意:personalPrefixString 值将从前缀字符串的属性文件条目中获取。 If prefixString value is testing and cacheName is student-details-cache then your cache key will be prefixed by: testing:student-details-cache::如果 prefixString 值是 testing 并且 cacheName 是 student-details-cache 那么你的缓存键的前缀是: testing:student-details-cache::
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.