2012-01-11 64 views

回答

0

春季3.1已经推出了缓存抽象。你应该能够利用这个来缓存你的DAO方法调用的结果。

该文档为here,它包含在Spring博客here中。

+0

感谢答复...请举一个例子或从中我可以实现链接。 – Anurag 2012-01-11 12:05:04

+0

有几个例子,但不是一个巨大的数字,因为它是一个相当新的功能。你已经阅读过文档了吗?自读完之后你有什么尝试?如果卡住了,请发布您的代码和任何错误。 – 2012-01-11 12:08:07

+0

http://www.brucephillips.name/blog/index.cfm/2011/3/22/Spring-31-Cache-Implementation-With-Cacheable-and-CacheEvict-Annotations – 2012-01-11 12:09:09

0

我有它的工作,但亚历克斯是正确的,有几种不同的方式来配置它,这取决于你想要作为你的缓存后端。

对于缓存配置,我选择了ehcache,因为它配置起来很简单,但具有配置ttl/etc的强大功能。

配置:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://www.springframework.org/schema/beans" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:cache="http://www.springframework.org/schema/cache" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.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"> 
     <property name="cacheManager"><ref local="ehcache"/></property> 
    </bean> 
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
      p:configLocation="classpath:ehcache.xml"/> 

</beans> 

ehcache.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" 
    updateCheck="false" monitoring="autodetect" 
    dynamicConfig="true"> 

<!-- http://ehcache.org/documentation/configuration.html --> 
<!-- Also See org.springframework.cache.ehcache.EhCacheFactoryBean --> 

    <diskStore path="java.io.tmpdir"/> 
    <cache name="resourceBundle" 
      overflowToDisk="false" 
      eternal="false" 
      maxElementsInMemory="500" 
      timeToIdleSeconds="86400" 
      timeToLiveSeconds="86400"/> 

</ehcache> 

我曾与我的junit环境中运行的Ehcache 2.5,由于与并行运行重名缓存问题是不允许的,他们似乎没有马上关闭,但这里是我的pom条目:

<dependency> 
     <groupId>net.sf.ehcache</groupId> 
     <artifactId>ehcache-core</artifactId> 
     <version>2.4.7</version> 
    </dependency> 

最后,在你的资料库,做到以下几点:

@Repository 
public class someStore { 
    @PersistenceContext 
    EntityManager em; 

    //The value here needs to match the name of the cache configured in your ehcache xml. You can also use Spel expressions in your key 
    @Cachable(value = "resourceBundle", key = "#basename+':'+#locale.toString()") 
    public ResourceBundle getResourceBundle(final String basename, final Locale locale){ 
     ... 
    } 

    @CacheEvict(value = "resourceBundle", key = "#basename+':'+#locale.toString()") 
    public void revertResourceBundle(final String basename, final Locale locale){ 
     ... 
    } 
} 
相关问题