小编Gos*_* U.的帖子

我应该如何在ehCache中使用@CachePut和@CacheEvict注释(ehCache 2.4.4,Spring 3.1.1)

我尝试了一些新的Spring功能,我发现@CachePut和@CacheEvict注释没有任何效果.可能是我做错了什么.你可以帮帮我吗?

我的applicationContext.xml.

<cache:annotation-driven />

<!--also tried this-->
<!--<ehcache: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"/>
Run Code Online (Sandbox Code Playgroud)

这部分效果很好.

@Cacheable(value = "finders")
public Finder getFinder(String code)
{
    return getFinderFromDB(code);
}

@CacheEvict(value = "finders", allEntries = true)
public void clearCache()
{
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我想从缓存中删除单个值或覆盖它,我不能这样做.我测试了什么:

@CacheEvict(value = "finders", key = "#finder.code")
public boolean updateFinder(Finder finder, boolean nullValuesAllowed)
{
    // ...
}

/////////////

@CacheEvict(value = "finders")
public void clearCache(String code)
{
}

/////////////

@CachePut(value = "finders", key = "#finder.code")
public Finder updateFinder(Finder finder, boolean nullValuesAllowed) …
Run Code Online (Sandbox Code Playgroud)

spring ehcache

13
推荐指数
1
解决办法
2万
查看次数

标签 统计

ehcache ×1

spring ×1