gpt4 book ai didi

java - Redis 缓存 Map

转载 作者:可可西里 更新时间:2023-11-01 11:14:36 27 4
gpt4 key购买 nike

我在使用 redis 缓存时遇到了问题:带有整数键的映射被序列化为字符串,如下所示:

 "1":"AAAA","2":"BBB","3":"CCC"

我的配置是这样的:

@Bean
public RedisCacheConfiguration myCacheConfiguration()
{
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ZERO)
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer(new Jackson2JsonRedisSerializer<>(Map.class)));
}

@Bean
public CacheManager myCacheManager(RedisConnectionFactory redisConnectionFactory)
{
return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(myCacheConfiguration())
.transactionAware()
.build();
}

我试图将 GenericJackson2JsonRedisSerializer 传递给 serializeValuesWith(),但它不起作用。有没有办法将 map 的整数键序列化\反序列化为数字?

最佳答案

Jackson2JsonRedisSerializerGenericJackson2JsonRedisSerializer 都允许使用自定义 ObjectMapper

不熟悉 Redis,但似乎是根据 doc 自定义序列化的设计方式:

Setting a custom-configured ObjectMapper is one way to take further control of the JSON serialization process. For example, an extended SerializerFactory can be configured that provides custom serializers for specific types. The other option for refining the serialization process is to use Jackson's provided annotations on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.

关于java - Redis 缓存 Map<Integer, String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55278446/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com