【问题标题】:cache database query in application server在应用服务器中缓存数据库查询
【发布时间】:2012-01-11 10:49:09
【问题描述】:

我有使用spring jdbc模板的webapps,现在想提高我的应用程序的性能,所以我想在我的Tomcat服务器中缓存一些DATABASE QUERY的结果。我如何实现dis概念。

谢谢

【问题讨论】:

    标签: mysql spring jdbctemplate


    【解决方案1】:

    Spring 3.1 引入了缓存抽象。您应该能够利用它来缓存 DAO 方法调用的结果。

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

    【讨论】:

    • 感谢您的回复...请给出我可以实施的示例或链接。
    • 有一些例子,但数量并不多,因为它是一个相当新的功能。您是否已经阅读过文档?读完之后你有什么尝试?如果您遇到困难,请发布您的代码和任何错误。
    【解决方案2】:

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

    对于缓存配置,我选择了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){
            ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 2017-04-10
      • 2011-07-27
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多