SpringBoot and EhCache - caches are not found

91 views Asked by At

I am using SpringBoot 3.1.5 Javax Cache API 1.1.1 and ehCache 3.8.1

My Cache Configuration looks like this

import java.time.Duration;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {

  @Bean
  public CacheManager configureCaches() {
    // Create CacheManager
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
    cacheManager.init();

    // Default Cache Configuration
    CacheConfigurationBuilder<Object, Object> defaultCacheConfiguration = CacheConfigurationBuilder
      .newCacheConfigurationBuilder(Object.class, Object.class, ResourcePoolsBuilder.heap(1000000))
      .withExpiry(
        // TODO: Add the time to idle if things work
//        ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(3))
          ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(6))
      );

    // Create Default Cache
    Cache<Object, Object> defaultCache = cacheManager.createCache("defaultCache", defaultCacheConfiguration);

    // Cache Configuration for LONGTERM
    CacheConfigurationBuilder<Object, Object> longTermCacheConfiguration = CacheConfigurationBuilder
      .newCacheConfigurationBuilder(Object.class, Object.class, ResourcePoolsBuilder.heap(1000000))
      .withExpiry(
//        ExpiryPolicyBuilder.timeToIdleExpiration(java.time.Duration.ofSeconds(300))
        ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(6))
      );

    // Create LONGTERM Cache
    Cache<Object, Object> longTermCache = cacheManager.createCache("LONGTERM", longTermCacheConfiguration);

    // Cache Configuration for FIXED_VALUES
    CacheConfigurationBuilder<Object, Object> fixedValuesCacheConfiguration = CacheConfigurationBuilder
      .newCacheConfigurationBuilder(Object.class, Object.class, ResourcePoolsBuilder.heap(1000000))
      .withExpiry(
        ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(6))

      );

    // Create FIXED_VALUES Cache
    Cache<Object, Object> fixedValuesCache = cacheManager.createCache("FIXED_VALUES", fixedValuesCacheConfiguration);

  return cacheManager;
  }
}

Debugging the line "return cacheManager" I can see that the 3 caches are populated, but when I try to use a cache

@Log4j2
@Component
class CachableObjectGenerator {

    @Cacheable(value = "FIXED_VALUES", key = "#id")
    public CachableObject getMessage(Long id) {
      log.info("In the getMessageMethod");
      return new CachableObject(id, "Hello World");
    }

  }

I get

java.lang.IllegalArgumentException: Cannot find cache named 'FIXED_VALUES' for Builder[public de.egf.accountplus.selfseviceproxy.config.CachableObject de.egf.accountplus.selfseviceproxy.config.CachableObjectGenerator.getMessage(java.lang.Long)] caches=[FIXED_VALUES] | key='#id' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

    at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:92)

and debugging AbstractCacheResolver shows that the CacheOperationInvocationContext is empty when calling method resolveCaches

Any suggestions to what I am missing?

Regards

0

There are 0 answers