【发布时间】:2012-01-11 10:49:09
【问题描述】:
我有使用spring jdbc模板的webapps,现在想提高我的应用程序的性能,所以我想在我的Tomcat服务器中缓存一些DATABASE QUERY的结果。我如何实现dis概念。
谢谢
【问题讨论】:
标签: mysql spring jdbctemplate
我有使用spring jdbc模板的webapps,现在想提高我的应用程序的性能,所以我想在我的Tomcat服务器中缓存一些DATABASE QUERY的结果。我如何实现dis概念。
谢谢
【问题讨论】:
标签: mysql spring jdbctemplate
【讨论】:
我有它的工作,但亚历克斯是正确的,有几种不同的方式来配置它,这取决于你想要什么作为你的缓存后端。
对于缓存配置,我选择了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){
...
}
}
【讨论】: