Redis 如何将值存储为 json
Redis how to store values as json
我在我的应用程序中使用 redis 作为我的缓存存储。为此,我使用以下 redis 配置:
@Configuration
@EnableCaching
public class SpringRedisConfig {
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setHostName("localhost");
connectionFactory.setPort(6379);
return connectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//template.setConnectionFactory(factory);
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
在我将数据存储在缓存中的服务中,我使用 @Cacheable 如下:
@Cacheable(value = "institute_detail_cache", key = "'inst_'+#instituteId")
public Map<String, String> getDetailsFromCache(Long instituteId) {
log.info("not found in cache.....");
Map<String, String> map = new HashMap<>();
map.put("name", "kayv");
map.put("age", "20");
return map;
}
代码运行良好并将数据作为对象存储在缓存中。这是我从 redis cli 获得的值:
但我的要求是使用键值对将数据(值)存储为 json。这就是为什么我使用 Jackson2JsonRedisSerializer 进行值序列化。
谁能指出我做错了什么?
我需要执行什么才能使我的 类 存储为 json 而不是我在此示例中使用的地图?
使用 GenericJackson2JsonRedisSerializer class 而不是 Jackson2JsonRedisSerializer class 解决了我的问题。
创建了一个新的配置 class (RedisCacheManagerConfiguration.java) 而不是旧的 SpringRedisConfig.java。
@Configuration
@EnableCaching
public class RedisCacheManagerConfiguration {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public CacheManager redisCacheManager() {
RedisSerializationContext.SerializationPair<Object> jsonSerializer =
RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer());
return RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory)
.cacheDefaults(
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(1))
.serializeValuesWith(jsonSerializer)
)
.build();
}
}
我在我的应用程序中使用 redis 作为我的缓存存储。为此,我使用以下 redis 配置:
@Configuration
@EnableCaching
public class SpringRedisConfig {
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setHostName("localhost");
connectionFactory.setPort(6379);
return connectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
//template.setConnectionFactory(factory);
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
在我将数据存储在缓存中的服务中,我使用 @Cacheable 如下:
@Cacheable(value = "institute_detail_cache", key = "'inst_'+#instituteId")
public Map<String, String> getDetailsFromCache(Long instituteId) {
log.info("not found in cache.....");
Map<String, String> map = new HashMap<>();
map.put("name", "kayv");
map.put("age", "20");
return map;
}
代码运行良好并将数据作为对象存储在缓存中。这是我从 redis cli 获得的值:
但我的要求是使用键值对将数据(值)存储为 json。这就是为什么我使用 Jackson2JsonRedisSerializer 进行值序列化。
谁能指出我做错了什么?
我需要执行什么才能使我的 类 存储为 json 而不是我在此示例中使用的地图?
使用 GenericJackson2JsonRedisSerializer class 而不是 Jackson2JsonRedisSerializer class 解决了我的问题。
创建了一个新的配置 class (RedisCacheManagerConfiguration.java) 而不是旧的 SpringRedisConfig.java。
@Configuration
@EnableCaching
public class RedisCacheManagerConfiguration {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public CacheManager redisCacheManager() {
RedisSerializationContext.SerializationPair<Object> jsonSerializer =
RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer());
return RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory)
.cacheDefaults(
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(1))
.serializeValuesWith(jsonSerializer)
)
.build();
}
}