我是Spring Data Redis的新手。我已经使用了spring boot数据redis 2.0.3,如下所示(从我的gradle构建中)
compile('org.springframework.boot:spring-boot-starter-data-redis:2.0.3.RELEASE')
compile('io.lettuce:lettuce-core:5.1.3.RELEASE')
当我尝试创建create(PUT)或get(GET)时,在Redis Desktop manager中注意到,密钥创建在上述存储桶中,但存储在存储桶下的空白命名空间(文件夹)中。但是我只想在存储桶(名称空间)下创建缓存键,而不要在空白子文件夹(如另一个空的命名空间)中创建(文件夹-从Redis Desktop Manager中可以看到)
@Bean
LettuceConnectionFactory lettuceClientConfigurationConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(redisHostName);
redisStandaloneConfiguration.setPort(redisPort);
redisStandaloneConfiguration.setPassword(RedisPassword.of(redisAuth));
redisStandaloneConfiguration.setDatabase(0);
LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfiguration = LettuceClientConfiguration.builder();
lettuceClientConfiguration.useSsl();
LettuceConnectionFactory lettuceConFactory = new LettuceConnectionFactory(redisStandaloneConfiguration,
lettuceClientConfiguration.build());
return lettuceConFactory;
}
@Bean
public RedisCacheConfiguration cacheConfiguration() {
RedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializer stringRedisSerializer = new StringRedisSerializer();
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues()
.entryTtl(Duration.ofHours(1))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer));
redisCacheConfiguration.usePrefix();
return redisCacheConfiguration;
}
@Bean
@Primary
public CacheManager initRedisCacheManager(LettuceConnectionFactory factory) {
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
.RedisCacheManagerBuilder.fromConnectionFactory(factory);
builder.cacheDefaults(cacheConfiguration());
builder.transactionAware();
RedisCacheManager cacheManager = builder.build();
cacheManager.afterPropertiesSet();
return cacheManager;
}
答案 0 :(得分:0)
我可以通过确保为每个缓存存储区设置prefixKyesWith来解决此问题,因此put和get操作会寻找正确的存储/读取位置。
/ ** *获取Redis缓存编写器 * @返回RedisCacheWriter * / @豆角,扁豆 RedisCacheWriter redisCacheWriter(){ 返回RedisCacheWriter.lockingRedisCacheWriter(lettuceClientConfigurationConnectionFactory()); }
/ ** *获取缓存管理器 * @返回CacheManager * / @豆角,扁豆 @主 CacheManager cacheManager(){ RedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); 映射cacheNamesConfigurationMap = new HashMap <>();
// configure catch buckets with prefixKeysWith information
cacheNamesConfigurationMap.put(CacheBucket.A, RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith(CacheBucket.A+":")
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer)));
cacheNamesConfigurationMap.put(CacheBucket.B, RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith(CacheBucket.B+":")
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer)));
cacheNamesConfigurationMap.put(CacheBucket.D, RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith(CacheBucket.D+":")
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer)));
cacheNamesConfigurationMap.put(CacheBucket.E, RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith(CacheBucket.E+":")
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer)));
cacheNamesConfigurationMap.put(CacheBucket.F, RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith(CacheBucket.F+":")
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer)));
RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter(), cacheConfiguration(), cacheNamesConfigurationMap);
cacheManager.setTransactionAware(true);
cacheManager.afterPropertiesSet();
return cacheManager;
}