EHCache 3 not able to enable caching. Getting exception if I used configuration without xml

320 Views Asked by At

I am trying to implement EHCache 3 with spring boot 3 without xml configuration. I did below configuration for EHCache 3

    @Bean
    public CacheManager getCacheManager() {
        CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true);
        CacheConfiguration<Integer, Double> configuration = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(Integer.class, Double.class, ResourcePoolsBuilder
                        .heap(100)
                        .offheap(10, MemoryUnit.MB))
                .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMinutes(10)))
                .build();
        
         Cache<Integer, Double> cache = cacheManager.createCache("demoCache", configuration);
         
         cache.put(1, 7d);
          Double value = cache.get(1);
          
          System.out.println("Cachce value::"+value);

//       String vaule = cache.get("pow_cache");

         return cacheManager;
    }

From service side method call is below

@Cacheable(value = "demoCache")
    public double demo() {
        return 52;
    }

If try to call this service getting below exception

java.lang.IllegalArgumentException: Cannot find cache named 'demoCache' for Builder[public double com.jts.ehcach.service.CalculationService.demo()] caches=[demoCache] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
    at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:92) ~[spring-context-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:270) ~[spring-context-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:775) ~[spring-context-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:283) ~[spring-context-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.<init>(CacheAspectSupport.java:666) ~[spring-context-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:363) ~[spring-context-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:64) ~[spring-context-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:765) ~[spring-aop-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:717) ~[spring-aop-6.1.0-M5.jar:6.1.0-M5]
    at com.jts.ehcach.service.CalculationService$$SpringCGLIB$$0.demo(<generated>) ~[classes/:na]
    at com.jts.ehcach.controller.CalculationRestController.areaOfCircle(CalculationRestController.java:26) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:253) ~[spring-web-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:181) ~[spring-web-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:918) ~[spring-webmvc-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:830) ~[spring-webmvc-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1086) ~[spring-webmvc-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) ~[spring-webmvc-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.1.0-M5.jar:6.1.0-M5]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) ~[spring-webmvc-6.1.0-M5.jar:6.1.0-M5]
    at jakarta.servlet.http.HttpServlet

Not able to understand why I am getting this not found caching name. Can someone please help me on this?

1

There are 1 best solutions below

0
Ken Chan On

Spring integrates with the EHCache3 through the JSR-107 JCache API which I quote from this :

Ehcache 3.x is fully JSR-107 compliant and no dedicated support is required for it. See JSR-107 Cache for details.

In term of springboot , you can customize the underlying javax.cache.CacheManager by defining a JCacheManagerCustomizer bean. In this customiser , you can then refer to this Ehcache3 documentation for how to configure javax.cache.cacheManager using Ehcache configuration.

So for your the cache configuration you shown , you should define the following JCacheManagerCustomizer bean instead :

@Bean
public JCacheManagerCustomizer jCacheManagerCustomizer() {
    return jcacheManager -> {
        CacheConfiguration<Integer, Double> ehCacheConfig = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(Integer.class, Double.class, ResourcePoolsBuilder
                        .heap(100)
                        .offheap(10, MemoryUnit.MB))
                .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMinutes(10)))
                .build();

       jcacheManager.createCache("demoCache",
                Eh107Configuration.fromEhcacheCacheConfiguration(ehCacheConfig));
    };
}

which cache the result of the following method :

@Cacheable(value = "demoCache")
public double demo(Integer int) {
       return 52;
 }

Note: I modify your demo function argument type a bit for better demonstrate my solution 's idea.