我正在使用Spring-boot + Redis将数据存储在缓存中。
我的控制器具有以下配置:
public class CacheController {
@GetMapping("/{key}")
@Cacheable(value = "myCacheValue", keyGenerator = "customKeyGenerator")
public String getByKey(@PathVariable("key") String key) {
return key + System.currentTimeMillis();
}
}
我的customKeyGenerator是:
public class CustomKeyGenerator implements KeyGenerator {
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getSimpleName())
.append("-")
.append(method.getName());
if (params != null) {
for (Object param : params) {
sb.append("-")
.append(param.getClass().getSimpleName())
.append(":").append(param);
}
}
return sb.toString();
}
}
当我连接Redis时,我具有以下密钥:
1 - redis-cli --raw
KEYS *
myCacheValue~keys
��t$CacheController-getByKey-String:key9
2 - redis-cli
KEYS *
myCacheValue~keys
\xac\xed\x00\x05t\x00$CacheController-getByKey-String:key9
为什么这些字符出现在“ CacheController”之前?
注意:我的密钥生成器以我的班级名称开头。
我希望我的密钥名称仅是这样: CacheController-getByKey-String:myKey
答案 0 :(得分:1)
在doc中,您可以使用RedisCacheConfiguration
Bean自定义redisCacheManager
:
@Configuration
public class CacheConfiguration {
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig();
}
}
,默认配置为:
public static RedisCacheConfiguration defaultCacheConfig() {
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
registerDefaultConverters(conversionService);
return new RedisCacheConfiguration(Duration.ZERO, true, true, CacheKeyPrefix.simple(),
SerializationPair.fromSerializer(new StringRedisSerializer()),// this will use string as the key serializer
SerializationPair.fromSerializer(new JdkSerializationRedisSerializer()), conversionService);
}
答案 1 :(得分:0)
我解决了我的问题。 我正在搜索并发现this post。
我更改了代码以使用RedisTemplate的某些属性:
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(cf);
configureSerializers(redisTemplate);
JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) cf;
JedisPoolConfig actualPoolConfig = jedisConnectionFactory.getPoolConfig();
jedisConnectionFactory.setPoolConfig(createJedisPoolConfig(actualPoolConfig));
return redisTemplate;
}
private void configureSerializers(RedisTemplate<String, String> redisTemplate) {
RedisSerializer<String> serializerKey = new StringRedisSerializer();
redisTemplate.setKeySerializer(serializerKey);
redisTemplate.setHashKeySerializer(serializerKey);
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
om.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jackson2JsonRedisSerializer.setObjectMapper(om);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
}
问题是因为默认的序列化器是“ JdkSerializationRedisSerializer”,现在我正在使用“ StringRedisSerializer”来生成密钥。 为了产生价值,我使用“ Jackson2JsonRedisSerializer”在JsonValue中转换数据。
现在,我的按键没有“奇怪”字符。