2012-01-11 63 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){ 
     ... 
    } 
} 
相關問題