简体   繁体   English

如何为非 spring 启动 java 应用程序设置 redis 缓存前缀键配置1823E6CC71

[英]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.

相关问题 Java Spring 引导如何设置 Redis Z594C103F5903E04E0C31DAZ8 上多个查询字符串参数的缓存键 - Java Spring Boot how to set Redis cache key for multiple query string parameters on controller 如何使用 Redis Cache 将数据存储在 java spring 启动应用程序中? - How to use Redis Cache to store the data in java spring boot application? 使用 Redis 支持的 Spring 引导缓存缓存不可序列化的对象 - Cache non-serializable objects using Spring Boot Cache backed by Redis 如何使用RedissonSessionManager设置Redis“会话密钥前缀” - How to set redis “session key prefix” using RedissonSessionManager Spring 启动 Redis 缓存删除缓存与多键 - Spring Boot Redis Cache remove cache with multiple key Redis 缓存实现使用 Spring 引导和散列 - Redis cache implementation using Spring boot with hashing 如何使用Java在xml中为属性设置前缀 - How to set prefix for the attribute in xml using java 如何在 Java 8 (Spring Boot) 应用程序中设置最大非堆内存? - How to set max non-heap memory in a Java 8 (Spring Boot) application? 为什么 Redis 缓存在我的 Spring 启动应用程序中没有变空? - Why Redis Cache is not getting empty in my Spring Boot application? 使用 Spring 禁用 redis 缓存,redission 客户端 - 没有 Spring 启动 - Disable redis cache using Spring, redission client - no Spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM