20 Ağustos 2021 Cuma

SpringCache Redis RedisCacheConfiguration Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.data.redis.cache.RedisCacheConfiguration;
disableCachingNullValues metodu
Örnek
Şöyle yaparız
@Bean
public RedisCacheConfiguration cacheConfiguration() {
  return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofMinutes(60))
    .disableCachingNullValues()
    .serializeValuesWith(RedisSerializationContext.SerializationPair
      .fromSerializer(new GenericJackson2JsonRedisSerializer()));
    }
}
entryTtl metodu
Örnek
Açıklaması şöyle
By setting the expiration time of different keys in the configuration file, the effect of customizing the key expiration time can be achieved.
Şöyle yaparız
@Autowired
private MyProperties properties;
 
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
  RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration
    .defaultCacheConfig()
    .entryTtl(Duration.ofMinutes(10)).disableCachingNullValues();
 
  RedisCacheWriter redisCacheWriter = RedisCacheWriter
    .nonLockingRedisCacheWriter(connectionFactory);
 
  ImmutableSet.Builder<String> cacheNames = ImmutableSet.builder();
  ImmutableMap.Builder<String, RedisCacheConfiguration> cacheConfig = 
    ImmutableMap.builder();
  for (Map.Entry<String, Duration> entry : properties.getInitCaches().entrySet()) {
    cacheNames.add(entry.getKey());
    cacheConfig.put(entry.getKey(), defaultCacheConfig.entryTtl(entry.getValue()));
  }
 
  return RedisCacheManager.builder(redisCacheWriter)
    .cacheDefaults(defaultCacheConfig)
    .initialCacheNames(cacheNames.build())
    .withInitialCacheConfigurations(cacheConfig.build())
    .build();
}
Properties dosyası şöyledir
initCaches:
  fooboo: 10m
  fooboo1: 1h
Properties sınıfı şöyledir
@ConfigurationProperties
public class MyProperties {
 
   private final Map<String, Duration> initCaches = Maps.newHashMap();
 
   public Map<String, Duration> getInitCaches() {
     return initCaches;
   }
}
serializeKeysWith ve serializeValuesWith metodları
Örnek - JSON
Şöyle yaparız. Burada ObjetMapper parametresi ile Spring'e ait olan bean kullanılıyor. Yeni bir tane yaratmaya gerek yok
@Configuration
@EnableCaching
class CachingConfig{

  @Bean
  fun cacheConfiguration(mapper: ObjectMapper): RedisCacheConfiguration {
    return RedisCacheConfiguration.defaultCacheConfig()
      .entryTtl(Duration.ofMinutes(5))
      .disableCachingNullValues()
      .serializeValuesWith(
        SerializationPair.fromSerializer(GenericJackson2JsonRedisSerializer(mapper))
      )
  }
}
Eğer Spring'e ait olan bean'ini kopyasın alıp yeni module takmak istersek şöyle yaparız
@Bean
fun cacheConfiguration(mapper: ObjectMapper): RedisCacheConfiguration {
  val myMapper = mapper.copy()
    .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
    .registerModule(
      Hibernate6Module()
        .enable(Hibernate6Module.Feature.FORCE_LAZY_LOADING)
        .enable(Hibernate6Module.Feature.REPLACE_PERSISTENT_COLLECTIONS)
      )
      .activateDefaultTyping(
        jacksonObjectMapper().polymorphicTypeValidator,
        ObjectMapper.DefaultTyping.EVERYTHING,
        JsonTypeInfo.As.PROPERTY
      )
  return RedisCacheConfiguration.defaultCacheConfig()
    .entryTtl(Duration.ofMinutes(5))
    .disableCachingNullValues()
    .serializeValuesWith(
      SerializationPair.fromSerializer(GenericJackson2JsonRedisSerializer(myMapper))
    )
}
Örnek - RedisSerialization
Şöyle yaparız
@EnableCaching
@Configuration
public class RedisConfig {

  private final RedisSerializer<String> stringSerializer = RedisSerializer.string();
  private final RedisSerializer<Object> objectRedisSerializer = RedisSerializer.json();

  private final Duration ttl = Duration.ofHours(6);

  @Bean
  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory
redisConnectionFactory) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    return redisTemplate;
  }

  @Bean
  public RedisCacheConfiguration redisCacheConfiguration() {
    return RedisCacheConfiguration
      .defaultCacheConfig()
      .serializeKeysWith(RedisSerializationContext.SerializationPair
.fromSerializer(stringSerializer))
      .serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer(objectRedisSerializer))
      .entryTtl(ttl);
  }
}

Hiç yorum yok:

Yorum Gönder