首页 文章

Spring 3.1 @Cacheable - 方法仍然执行

提问于
浏览
28

我正在尝试实现Spring 3.1缓存,如herehere所解释,但它似乎不起作用:我的方法每次都会运行,即使它被标记为@cacheable . 我究竟做错了什么?

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

spring 试验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语句之间没有缓存检查 .

有什么我想念的吗?

3 回答

  • 80

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

    在代理模式(默认设置)下,只拦截通过代理进入的外部方法调用 . 这意味着实际上,自调用目标对象中的一个方法调用目标对象的另一个方法,即使调用的方法用@Cacheable标记,也不会在运行时导致实际的缓存 - 考虑使用aspectj模式这个案例 .

    方法可见性和@ Cacheable / @ CachePut / @ CacheEvict使用代理时,只应将@Cache注释应用于具有公共可见性的方法 .

    • 您在同一目标对象中自行调用 someMethod .

    • 您的 @Cacheable 方法不公开 .

  • 1

    您需要定义一个与您在注释中引用的名称匹配的缓存(“testmethod”) . 在ehcache.xml中为该缓存创建一个条目 .

  • 2

    除了Lee Chee Kiam:这是我的小型项目的解决方案,只有边际使用绕过(未注释)方法调用 . The DAO is simply injected into itself as a proxy and calls it's own methods using that proxy instead of a simple method call. 因此考虑@Cacheable而不进行复杂的操作 .

    强烈建议使用代码内文档,因为它对同事来说可能看起来很奇怪 . 但它易于测试,简单,快速实现,并使我完全成熟的AspectJ仪器 . 然而,对于更多的使用,我也建议像Lee Chee Kiam那样使用AspectJ解决方案 .

    @Service
    @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
    class PersonDao {
    
        private final PersonDao _personDao;
    
        @Autowired
        public PersonDao(PersonDao personDao) {
            _personDao = personDao;
        }
    
        @Cacheable(value = "defaultCache", key = "#id")
        public Person findPerson(int id) {
            return getSession().getPerson(id);
        }
    
        public List<Person> findPersons(int[] ids) {
            List<Person> list = new ArrayList<Person>();
            for (int id : ids) {
                list.add(_personDao.findPerson(id));
            }
            return list;
        }
    }
    

相关问题