jeudi 1 août 2019

How to combine EhCache with Redis caches?

I have a Spring Boot project with already implemented EhCache configuration:

spring:
  cache:
    ehcache:
      config: classpath:application-ehcache.xml

and a snippet of ...-ehcache.xml looks like:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd"
         updateCheck="true"
         monitoring="autodetect"
         dynamicConfig="true">

    <cache name="airportbyicao"
           maxEntriesLocalHeap="500"
           eternal="false"
           overflowToDisk="false"
           diskPersistent="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           diskExpiryThreadIntervalSeconds="1"
           memoryStoreEvictionPolicy="FIFO">
    </cache>

And cache for service looks following:

@Cacheable(cacheNames = "airportbyicao")
public Optional<AirportData> findAirportFromIcao(String code) {
...

Everything works fine at this point.
Now I want to add ElastiCache with Redis to it.

I updated gradle.build:

    compile 'org.springframework.boot:spring-boot-starter-data-redis'
    compile 'org.springframework.cloud:spring-cloud-aws-context'
    compile 'net.sf.ehcache:ehcache:2.9.1'
    compile 'javax.cache:cache-api:1.1.0'
    compile 'redis.clients:jedis:2.10.1'

added configuration class:

@Configuration
public class RedisConfig {

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

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

    @Primary
    @Bean(name = "cacheManager")
    public CacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
        return RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(RedisCacheConfiguration.defaultCacheConfig())
                .build();
    }
}

set to application.yml:

spring:
  redis:
    host: localhost
    port: 6379

And now when I want to run any request to service I got:

{
  "error": "Cannot find cache named 'vbookings' for Builder[public abstract org.springframework.data.domain.Page com.chartermatch.booking.data.VBookingRepository.findAll(com.querydsl.core.types.Predicate,org.springframework.data.domain.Pageable)] caches=[vbookings] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'"
}

I couldn't understand what is missing there. I even didn't add any Redis cache implementation.

How to make coexisting those caches together in one project?




Aucun commentaire:

Enregistrer un commentaire