Search in sources :

Example 1 with RedisCacheConfiguration

use of org.springframework.data.redis.cache.RedisCacheConfiguration in project spring-boot by spring-projects.

the class CacheAutoConfigurationTests method redisCacheWithRedisCacheManagerBuilderCustomizer.

@Test
void redisCacheWithRedisCacheManagerBuilderCustomizer() {
    this.contextRunner.withUserConfiguration(RedisWithRedisCacheManagerBuilderCustomizerConfiguration.class).withPropertyValues("spring.cache.type=redis", "spring.cache.redis.time-to-live=15000").run((context) -> {
        RedisCacheManager cacheManager = getCacheManager(context, RedisCacheManager.class);
        RedisCacheConfiguration redisCacheConfiguration = getDefaultRedisCacheConfiguration(cacheManager);
        assertThat(redisCacheConfiguration.getTtl()).isEqualTo(java.time.Duration.ofSeconds(10));
    });
}
Also used : RedisCacheConfiguration(org.springframework.data.redis.cache.RedisCacheConfiguration) RedisCacheManager(org.springframework.data.redis.cache.RedisCacheManager) Test(org.junit.jupiter.api.Test)

Example 2 with RedisCacheConfiguration

use of org.springframework.data.redis.cache.RedisCacheConfiguration in project thingsboard by thingsboard.

the class TBRedisCacheConfiguration method cacheManager.

/**
 * Transaction aware RedisCacheManager.
 * Enable RedisCaches to synchronize cache put/evict operations with ongoing Spring-managed transactions.
 */
@Bean
public CacheManager cacheManager(RedisConnectionFactory cf) {
    DefaultFormattingConversionService redisConversionService = new DefaultFormattingConversionService();
    RedisCacheConfiguration.registerDefaultConverters(redisConversionService);
    registerDefaultConverters(redisConversionService);
    RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig().withConversionService(redisConversionService);
    return RedisCacheManager.builder(cf).cacheDefaults(configuration).transactionAware().build();
}
Also used : RedisCacheConfiguration(org.springframework.data.redis.cache.RedisCacheConfiguration) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Bean(org.springframework.context.annotation.Bean)

Example 3 with RedisCacheConfiguration

use of org.springframework.data.redis.cache.RedisCacheConfiguration in project spring-boot by spring-projects.

the class CacheAutoConfigurationTests method redisCacheWithRedisCacheConfiguration.

@Test
void redisCacheWithRedisCacheConfiguration() {
    this.contextRunner.withUserConfiguration(RedisWithCacheConfigurationConfiguration.class).withPropertyValues("spring.cache.type=redis", "spring.cache.redis.time-to-live=15000", "spring.cache.redis.keyPrefix=foo").run((context) -> {
        RedisCacheManager cacheManager = getCacheManager(context, RedisCacheManager.class);
        assertThat(cacheManager.getCacheNames()).isEmpty();
        RedisCacheConfiguration redisCacheConfiguration = getDefaultRedisCacheConfiguration(cacheManager);
        assertThat(redisCacheConfiguration.getTtl()).isEqualTo(java.time.Duration.ofSeconds(30));
        assertThat(redisCacheConfiguration.getKeyPrefixFor("")).isEqualTo("bar::");
    });
}
Also used : RedisCacheConfiguration(org.springframework.data.redis.cache.RedisCacheConfiguration) RedisCacheManager(org.springframework.data.redis.cache.RedisCacheManager) Test(org.junit.jupiter.api.Test)

Example 4 with RedisCacheConfiguration

use of org.springframework.data.redis.cache.RedisCacheConfiguration in project spring-boot by spring-projects.

the class CacheAutoConfigurationTests method redisCacheExplicit.

@Test
void redisCacheExplicit() {
    this.contextRunner.withUserConfiguration(RedisConfiguration.class).withPropertyValues("spring.cache.type=redis", "spring.cache.redis.time-to-live=15000", "spring.cache.redis.cacheNullValues=false", "spring.cache.redis.keyPrefix=prefix", "spring.cache.redis.useKeyPrefix=true").run((context) -> {
        RedisCacheManager cacheManager = getCacheManager(context, RedisCacheManager.class);
        assertThat(cacheManager.getCacheNames()).isEmpty();
        RedisCacheConfiguration redisCacheConfiguration = getDefaultRedisCacheConfiguration(cacheManager);
        assertThat(redisCacheConfiguration.getTtl()).isEqualTo(java.time.Duration.ofSeconds(15));
        assertThat(redisCacheConfiguration.getAllowCacheNullValues()).isFalse();
        assertThat(redisCacheConfiguration.getKeyPrefixFor("MyCache")).isEqualTo("prefixMyCache::");
        assertThat(redisCacheConfiguration.usePrefix()).isTrue();
    });
}
Also used : RedisCacheConfiguration(org.springframework.data.redis.cache.RedisCacheConfiguration) RedisCacheManager(org.springframework.data.redis.cache.RedisCacheManager) Test(org.junit.jupiter.api.Test)

Example 5 with RedisCacheConfiguration

use of org.springframework.data.redis.cache.RedisCacheConfiguration in project spring-boot by spring-projects.

the class CacheAutoConfigurationTests method redisCacheExplicitWithCaches.

@Test
void redisCacheExplicitWithCaches() {
    this.contextRunner.withUserConfiguration(RedisConfiguration.class).withPropertyValues("spring.cache.type=redis", "spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar").run((context) -> {
        RedisCacheManager cacheManager = getCacheManager(context, RedisCacheManager.class);
        assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar");
        RedisCacheConfiguration redisCacheConfiguration = getDefaultRedisCacheConfiguration(cacheManager);
        assertThat(redisCacheConfiguration.getTtl()).isEqualTo(java.time.Duration.ofMinutes(0));
        assertThat(redisCacheConfiguration.getAllowCacheNullValues()).isTrue();
        assertThat(redisCacheConfiguration.getKeyPrefixFor("test")).isEqualTo("test::");
        assertThat(redisCacheConfiguration.usePrefix()).isTrue();
    });
}
Also used : RedisCacheConfiguration(org.springframework.data.redis.cache.RedisCacheConfiguration) RedisCacheManager(org.springframework.data.redis.cache.RedisCacheManager) Test(org.junit.jupiter.api.Test)

Aggregations

RedisCacheConfiguration (org.springframework.data.redis.cache.RedisCacheConfiguration)5 Test (org.junit.jupiter.api.Test)4 RedisCacheManager (org.springframework.data.redis.cache.RedisCacheManager)4 Bean (org.springframework.context.annotation.Bean)1 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)1