无法为 CacheableOperation[] 缓存找到名为“”的缓存

Cannot find cache named '' for CacheableOperation[] caches

我的错误是:

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$a0d8a.getActionsByCasId(<generated>)
    at com.codinko.caching.EmployeeDAO.getActionBycasId(EmployeeDAO.java:47)
    at com.codinko.caching.EmployeeDAO$$FastClassBySpringCGLIB$1aa49b.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$99d753.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" />    

即使我使用了 annotation,ehcache.xml 文件中是否需要上述配置????

试试这个:

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

如果您使用 spring 云 aws,请禁用自动 elasticache 配置。

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

只是挑剔使用 @SpringBootApplication 而不是 @EnableAutoConfiguration

上面的排除选项对我不起作用。但是,下面的工作。! 如果您使用的是 spring aws 云,则像下面这样排除弹性。

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

尝试更改

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

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

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