org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair Java Examples

The following examples show how to use org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: spring-boot-start-current   Author: yujunhao8831   File: SpringCacheConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public RedisCacheConfiguration redisCacheConfiguration ( Jackson2JsonRedisSerializer jackson2JsonRedisSerializer ) {
	RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
	return config.entryTtl( Duration.ofSeconds( defaultExpiration ) )
				 .serializeKeysWith( SerializationPair.fromSerializer( new StringRedisSerializer() ) )
				 .serializeValuesWith( SerializationPair.fromSerializer( jackson2JsonRedisSerializer ) );
}
 
Example #2
Source Project: loc-framework   Author: lord-of-code   File: RedisCacheAutoConfiguration.java    License: MIT License 5 votes vote down vote up
private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration() {
  Redis redisProperties = this.cacheProperties.getRedis();
  org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
      .defaultCacheConfig();

  Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
      Object.class);

  ObjectMapper om = new ObjectMapper();
  om.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);
  om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  om.setSerializationInclusion(Include.NON_NULL);
  om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

  jackson2JsonRedisSerializer.setObjectMapper(om);
  config = config
      .serializeKeysWith(SerializationPair.fromSerializer(new StringRedisSerializer()));
  config = config
      .serializeValuesWith(SerializationPair.fromSerializer(jackson2JsonRedisSerializer));

  if (redisProperties.getTimeToLive() != null) {
    config = config.entryTtl(redisProperties.getTimeToLive());
  }
  if (redisProperties.getKeyPrefix() != null) {
    config = config.prefixKeysWith(redisProperties.getKeyPrefix());
  }
  if (!redisProperties.isCacheNullValues()) {
    config = config.disableCachingNullValues();
  }
  if (!redisProperties.isUseKeyPrefix()) {
    config = config.disableKeyPrefix();
  }
  return config;
}