Spring 3.1 @Cacheable - 方法仍然执行

标签 spring caching ehcache

我正在尝试按照 here 的说明实现 Spring 3.1 缓存。和 here ,但它似乎不起作用:我的方法每次都运行,即使它被标记为@cacheable。我做错了什么?

我已将其移至带有自己的配置文件的 junit 测试用例中,以将其与我的应用程序的其余部分隔离,但问题仍然存在。以下是相关文件:

Spring-test-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:cache="http://www.springframework.org/schema/cache"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
      p:config-location="classpath:ehcache.xml"/>
</beans>

ehcache.xml

<ehcache>
<diskStore path="java.io.tmpdir"/>
<cache name="cache"
       maxElementsInMemory="100"
       eternal="false"
       timeToIdleSeconds="120"
       timeToLiveSeconds="120"
       overflowToDisk="true"
       maxElementsOnDisk="10000000"
       diskPersistent="false"
       diskExpiryThreadIntervalSeconds="120"
       memoryStoreEvictionPolicy="LRU"/>

</ehcache>

MyTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-test-servlet.xml"})
@Component
public class MyTest extends TestCase {

    @Test
    public void testCache1(){
        for(int i = 0; i < 5; i++){
            System.out.println("Calling someMethod...");
            System.out.println(someMethod(0));
        }
    }

    @Cacheable("testmethod")
    private int someMethod(int val){
        System.out.println("Not from cache");
        return 5;
    }
}

相关 Pom 条目:(spring-version = 3.1.1.RELEASE)

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>

当我运行测试时,Spring 会发出一些调试消息,看起来我的缓存已初始化且没有错误

DEBUG: config.ConfigurationHelper - No CacheManagerEventListenerFactory class specified. Skipping...
DEBUG: ehcache.Cache - No BootstrapCacheLoaderFactory class specified. Skipping...
DEBUG: ehcache.Cache - CacheWriter factory not configured. Skipping...
DEBUG: config.ConfigurationHelper - No CacheExceptionHandlerFactory class specified. Skipping...
DEBUG: store.MemoryStore - Initialized net.sf.ehcache.store.MemoryStore for cache
DEBUG: disk.DiskStorageFactory - Failed to delete file cache.data
DEBUG: disk.DiskStorageFactory - Failed to delete file cache.index
DEBUG: disk.DiskStorageFactory - Matching data file missing (or empty) for index file. Deleting index file /var/folders/qg/xwdvsg6x3mx_z_rcfvq7lc0m0000gn/T/cache.index
DEBUG: disk.DiskStorageFactory - Failed to delete file cache.index
DEBUG: ehcache.Cache - Initialised cache: cache
DEBUG: config.ConfigurationHelper - CacheDecoratorFactory not configured. Skipping for 'cache'.
DEBUG: config.ConfigurationHelper - CacheDecoratorFactory not configured for defaultCache. Skipping for 'cache'.

但调试输出显示在对 someMethod 的方法调用和每次从 someMethod 内部打印的 print 语句之间没有缓存检查。

我有什么遗漏吗?

最佳答案

来自 http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/cache.html

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual caching at runtime even if the invoked method is marked with @Cacheable - considering using the aspectj mode in this case.

Method visibility and @Cacheable/@CachePut/@CacheEvict

When using proxies, you should apply the @Cache annotations only to methods with public visibility.

  1. 您在同一目标对象中自行调用 someMethod
  2. 您的 @Cacheable 方法未公开。

关于Spring 3.1 @Cacheable - 方法仍然执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10343885/

相关文章:

java - "on-heap"和 "off-heap"之间的区别

java - 在 JUnit 测试中使用事务注释时数据未保存在数据库中

java - 如何在登录表单中添加spring security

c# - 多个客户端使用redis缓存

qt - 可以为特定项目禁用 Qt 5.8 中的 QML 缓存吗?

Java应用程序对象...它存储在哪里?

hibernate - 线程自旋等待时间以传递错误

java.lang.NoSuchMethodError : 'void org. thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor

java - Spring Batch FlatFileItemWriter 仅在第二次运行后处理数据

javascript - 是否有类似 Jquery 内存或缓存的东西?