Giriş
Şu satırı dahil ederiz.
Örnek - custom serializer
Şu satırı dahil ederiz.
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
Kullanım
Bu sınıf builder örüntüsünü kullanıyor. Dolayısıyla önce
1. builder(RedisConnectonFactory) çağrısıyla bir builder elde ediliyor.
2. Daha sonra bu nesneye değerler atanıyor.
3. En sonunda da build() çağrısıyla bir CacheManager elde ediliyor.
builder metodu - RedisConnectonFactory
Örnek
Şöyle yaparız
@Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSupport { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private int redisPort; @Bean public JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(new RedisStandaloneConfiguration(redisHost, redisPort)); } @Bean public RedisCacheManager cacheManager() { return RedisCacheManager.builder(jedisConnectionFactory()).build(); } }
Örnek
Şöyle yaparız
@Bean public LettuceConnectionFactory redisConnectionFactory() { ... } @Bean public RedisCacheConfiguration cacheConfiguration() { ... } private Map<String, RedisCacheConfiguration> getCustomRedisCacheConfigurations() { } @Bean public CacheManager cacheManager() { return RedisCacheManager.builder(redisConnectionFactory()) .cacheDefaults(cacheConfiguration()) .withInitialCacheConfigurations(getCustomRedisCacheConfigurations())//Initial caches .build(); }
cacheDefaults metodu - RedisCacheConfiguration
Şöyle yaparız. Key için string, value için JSON kullanılır
@Beanpublic CacheManager cacheManager(RedisConnectionFactory factory) {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));RedisCacheManager redisCacheManager = RedisCacheManager.builder(factory).cacheDefaults(redisCacheConfiguration).build();return redisCacheManager;}
Örnek - custom serializer
Şöyle yaparız. Key için Java, value için JSON kullanılır
@Bean public RedisCacheManager cacheManager() { return RedisCacheManager.builder(this.redisConnectionFactory()) .cacheDefaults(this.cacheConfiguration()) .build(); } @Bean public RedisCacheConfiguration cacheConfiguration() { return RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofMinutes(redisTimeToLive)) .disableCachingNullValues() .serializeValuesWith(SerializationPair .fromSerializer(new GenericJackson2JsonRedisSerializer())); }
Örnek - custom TTL
Şöyle yaparız.
initialCacheNames metodu
Şöyle yaparız.
@Bean
public RedisCacheConfiguration cacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(600))
.disableCachingNullValues();
}
@Bean
public RedisCacheManager cacheManager() {
Map<String, RedisCacheConfiguration> cacheConfigs = ...
return RedisCacheManager.builder(redisConnectionFactory())
.cacheDefaults(cacheConfiguration())
.withInitialCacheConfigurations(cacheConfigs)
.transactionAware()
.build();
}
fromConnectionFactory metodu
Örnek
Şöyle yaparız
@EnableCaching @Configuration public class CacheableConfiguration { @Value("${redis.service.cacheable.prefix}") private String serviceCacheablePrefix; @Value("${redis.service.cacheable.ttl}") private int serviceCacheableTtl; @Bean RedisCacheWriter redisCacheWriter() { return RedisCacheWriter.lockingRedisCacheWriter(lettuceConnectionFactory()); } @Bean public CacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory){ var redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofMinutes(serviceCacheableTtl)) .prefixCacheNameWith(serviceCacheablePrefix); return RedisCacheManager.RedisCacheManagerBuilder .fromConnectionFactory(lettuceConnectionFactory) .cacheDefaults(redisCacheConfiguration) .build(); } }
Şöyle yaparız.
RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
.initialCacheNames(Set.of("cacheOne", "cacheTwo"))
.build();
Hiç yorum yok:
Yorum Gönder