Cannot find cache named '' for CacheableOperation[] caches
Asked Answered
D

10

30

My error is :

Exception in thread "main" java.lang.IllegalArgumentException: Cannot find cache named 'getActionsBycasId' for CacheableOperation[public java.util.List com.codinko.database.DataBaseConnection.getActionsByCasId(int)] caches=[getActionsBycasId] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless=''
    at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:81)
    at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:214)
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:553)
    at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:227)
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.<init>(CacheAspectSupport.java:498)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
    at com.codinko.database.DataBaseConnection$$EnhancerBySpringCGLIB$$21a0d8a.getActionsByCasId(<generated>)
    at com.codinko.caching.EmployeeDAO.getActionBycasId(EmployeeDAO.java:47)
    at com.codinko.caching.EmployeeDAO$$FastClassBySpringCGLIB$$191aa49b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
    at com.codinko.caching.EmployeeDAO$$EnhancerBySpringCGLIB$$3399d753.getActionBycasId(<generated>)
    at com.codinko.caching.Main.main(Main.java:22)    

My function is :

@Cacheable("getActionsBycasId")
public List<SMSAction> getActionsByCasId(int casId){
    System.out.println("Inside getActionsByCasId");
    //My logic
    return list;
}    

when i add below on ehcache.xml then above error not comes but can't know why this error come .

<cache name="getActionsBycasId" maxElementsInMemory="50" eternal="false"
    overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />    

Is this above configuration required in ehcache.xml file even though i used annotation ????

Declivity answered 19/1, 2015 at 8:7 Comment(2)
The point is that you use the @Cacheable annotation to instruct Spring to cache the result in the cache named "getActionsBycasId". However, until you configure this cache via the Ehcache configuration file, you don't have a cache named "getActionsBycasId".Alpert
The @Cacheable annotation doesn't work without separate configuration (which can be done in various ways in modern Spring Boot).Amaranthine
P
31

Try this :

@Bean
public CacheManager cacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    List<Cache> caches = new ArrayList<Cache>();
    caches.add(new ConcurrentMapCache("getActionsBycasId"));
    cacheManager.setCaches(caches);
    return cacheManager;
}
Physoclistous answered 9/4, 2015 at 8:0 Comment(2)
The OP was using Ehcache. How come this answer has so many votes if it resorts to using another cache (concurrent map cache)?Ratliff
@Primary @Bean in my case watch this. javabeat.net/spring-cacheHoley
A
11

If you use spring cloud aws, disable automatic elasticache configuration.

@EnableAutoConfiguration(exclude = ElastiCacheAutoConfiguration.class)
@EnableCaching
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Ablation answered 23/5, 2016 at 20:44 Comment(1)
Thanks that solved my problem. It is weird though that this issue does not happen if my application runs on a local box but only happens when the application is deployed to EC2.Licking
U
5
@SpringBootApplication(exclude = {
        ContextStackAutoConfiguration.class,
        ElastiCacheAutoConfiguration.class
})

Just being nit-picky use @SpringBootApplication instead of @EnableAutoConfiguration

Unset answered 3/10, 2016 at 9:29 Comment(0)
G
1

Add ehcache.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config xmlns='http://www.ehcache.org/v3'>

        <persistence directory="${java.io.tmpdir}" />

        <!-- Default cache template -->
        <cache-template name="default">
            <expiry>
                <tti unit="hours">4</tti>
                <!-- <ttl unit="minutes">2</ttl> -->
            </expiry>
            <listeners>
                <listener>
                    <class>com.org.lob.support.LoggingTaskCacheListener</class>
                    <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                    <event-ordering-mode>UNORDERED</event-ordering-mode>
                    <events-to-fire-on>CREATED</events-to-fire-on>
                    <events-to-fire-on>EXPIRED</events-to-fire-on>
                    <events-to-fire-on>REMOVED</events-to-fire-on>
                    <events-to-fire-on>UPDATED</events-to-fire-on>
                </listener>
            </listeners>
            <resources>
                <heap unit="MB">10</heap>
                <offheap unit="MB">50</offheap>
                <disk persistent="true" unit="GB">1</disk>
            </resources>
            <!-- 
            <heap-store-settings>
                <max-object-graph-size>2000</max-object-graph-size>
                <max-object-size unit="kB">5</max-object-size>
            </heap-store-settings>
            -->
        </cache-template>

        <!-- Cache configurations -->
        <cache alias="books" uses-template="default" >
            <key-type>java.lang.String</key-type>
            <value-type>com.org.lob.project.repository.entity.Book</value-type>     
        </cache>

        <cache alias="files" uses-template="default" >
            <key-type>java.lang.String</key-type>
            <value-type>java.lang.String</value-type>       
        </cache>

    </config>

Update your application.properties

 spring.cache.jcache.config=classpath:ehcache.xml
Greenroom answered 14/8, 2021 at 18:21 Comment(0)
A
0

Try changing

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  <property name="cacheManagerName" value="cacheManager_service" />
  <property name="configLocation" value="classpath:ehcache-services.xml" />
  <property name="shared" value="true" />
</bean>

To

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" primary="true">
  <property name="cacheManagerName" value="cacheManager_service" />
  <property name="configLocation" value="classpath:ehcache-services.xml" />
</bean>
Alkylation answered 31/1, 2019 at 12:34 Comment(0)
K
0

You can define ehcache.xml in resources folder and consider configuring ,, tags and set alias as your cache able method. It worked for me.

Kunlun answered 22/7, 2019 at 12:17 Comment(0)
K
0

As you have in your class path resource an ehcache.xml file already with configurations, the best way there is to initialize a bean with this file configuration:

  @Bean
  public EhCacheManagerFactoryBean ehCacheCacheManager() {
    final EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
    ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
    ehCacheManagerFactoryBean.setShared(true);
    return ehCacheManagerFactoryBean;
  }

  @Bean
  public CacheManager cacheManager() {
    return new EhCacheCacheManager(ehCacheCacheManager().getObject());
  }
Kelcie answered 16/6, 2022 at 21:29 Comment(0)
H
0

just create a @Bean like this in your unit-test's configurations:

@Bean
public CacheManager cacheManager() {
  return new ConcurrentMapCacheManager("cacheName1", "cacheName2", "cacheName3", "cacheNameN");
}
Ha answered 14/11, 2022 at 10:30 Comment(0)
H
0

For caffeine it could be fixed in application.yaml:

spring:
  cache:
    cache-names: getActionsBycasId, ...
Humperdinck answered 23/1, 2024 at 15:6 Comment(0)
S
-1

The above exclude option, didnt work for me. But, the below worked.!! If you are using spring aws cloud, then exclude the elastic like below.

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-messaging</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.amazonaws</groupId>
                    <artifactId>
                        elasticache-java-cluster-client
                    </artifactId>
                </exclusion>
            </exclusions>
        </dependency>
Sutherlan answered 7/3, 2017 at 18:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.