无法找到名为''的缓存用于CacheableOperation[] caches。

28

我的错误是:

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)    

我的函数是:

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

当我在ehcache.xml文件中添加下面的内容时,就不会出现上述错误了,但是我不知道为什么会出现这个错误。

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

即使我使用了注解,上述配置是否仍然需要在ehcache.xml文件中?


6
重点是你使用@Cacheable注解指示Spring将结果缓存到名为“getActionsBycasId”的缓存中。然而,在通过Ehcache配置文件配置此缓存之前,你没有名为“getActionsBycasId”的缓存。 - John Donn
@Cacheable注解在没有单独配置的情况下无法工作(在现代Spring Boot中可以通过多种方式进行配置)。 - Brent Bradburn
9个回答

30

试试这个:

@Bean
public CacheManager cacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    List<Cache> caches = new ArrayList<Cache>();
    caches.add(new ConcurrentMapCache("getActionsBycasId"));
    cacheManager.setCaches(caches);
    return cacheManager;
}

15
OP使用的是Ehcache,但这个答案却使用了另一种缓存(并发映射缓存),为什么会有这么多投票? - Inego
1
@Primary @Bean 在我的情况下,请看这个链接。 https://javabeat.net/spring-cache/ - seunggabi

11

如果您使用Spring Cloud AWS,请禁用自动ElastiCache配置。

@EnableAutoConfiguration(exclude = ElastiCacheAutoConfiguration.class)
@EnableCaching
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

谢谢,问题已解决。不过很奇怪的是,如果我的应用程序在本地运行,这个问题就不会出现,只有当应用程序部署到 EC2 上时才会出现。 - kca2ply

5
@SpringBootApplication(exclude = {
        ContextStackAutoConfiguration.class,
        ElastiCacheAutoConfiguration.class
})

仅仅是一些小细节,使用@SpringBootApplication代替@EnableAutoConfiguration


1
添加 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>

更新你的application.properties文件

 spring.cache.jcache.config=classpath:ehcache.xml

0

如果您的类路径资源中已经有一个带有配置的ehcache.xml文件,那么最好的方法是使用此文件配置初始化一个bean:

  @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());
  }

0

只需在您的单元测试配置中创建一个像这样的@Bean:

@Bean
public CacheManager cacheManager() {
  return new ConcurrentMapCacheManager("cacheName1", "cacheName2", "cacheName3", "cacheNameN");
}

0
你可以在资源文件夹中定义ehcache.xml,并考虑配置、标签,并将别名设置为可缓存的方法。这对我很有效。

0

尝试更改

<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>

<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>

-1

上述的排除选项对我没有起作用。但是下面的方法有效! 如果您正在使用Spring AWS Cloud,则可以像下面这样排除弹性。

<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接