我有使用spring jdbc模板的webapps,現在想提高我的應用程序的性能,所以我想緩存一些DATABASE QUERY在Tomcat服務器中的結果。如何實現dis概念。在應用程序服務器中緩存數據庫查詢
感謝
我有使用spring jdbc模板的webapps,現在想提高我的應用程序的性能,所以我想緩存一些DATABASE QUERY在Tomcat服務器中的結果。如何實現dis概念。在應用程序服務器中緩存數據庫查詢
感謝
我有它的工作,但亞歷克斯是正確的,有幾種不同的方式來配置它,這取決於你想要作爲你的緩存後端。
對於緩存配置,我選擇了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){
...
}
}
感謝答覆...請舉一個例子或從中我可以實現鏈接。 – Anurag 2012-01-11 12:05:04
有幾個例子,但不是一個巨大的數字,因爲它是一個相當新的功能。你已經閱讀過文檔了嗎?自讀完之後你有什麼嘗試?如果卡住了,請發佈您的代碼和任何錯誤。 – 2012-01-11 12:08:07
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