SpringMVC + EChache配置后不能缓存的问题

刚刚用到EChache整合SpringMVC,用到的Marven

1、org.springframework:spring-webmvc:4.3.2.RELEASE

2、org.springframework:spring-context-support:4.3.4.RELEASE

3、net.sf.ehcache:ehcache-web:2.0.4

    一、SpringMVC+Ehcachesql缓存

    1、配置XML,在applicationContext.xml配置文件中加上以下代码:

<!--ehcache配置-->
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcacheFactory"></property>
</bean>
<bean id="ehcacheFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:xmlconfig/ehcache-setting.xml"></property>
</bean>

    2、配置ehcache-setting.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="true" monitoring="autodetect" dynamicConfig="true">
    <diskStore path="E://tmpdir"/>
   <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="1000000"
            timeToLiveSeconds="1000000"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
   </defaultCache>
    <cache name="getTable"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="20"
           timeToLiveSeconds="20"
           overflowToDisk="true"
           maxElementsOnDisk="10000000"
           diskPersistent="false"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU" />
</ehcache>

    3、通过注释的方式

    在Service中通过@Cacheable注解,此处的getTable,在ehcache-setting.xml中定义。

@Cacheable(value = "getTable")
public List<String> getDept(){
    return configService.getDept();
}

 

    二、jsp+ehcache缓存整个页面

    1、在web.xml中添加代码

<filter>
    <filter-name>PageCachingFilter</filter-name>
    <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>
    <init-param>
        <param-name>cacheName</param-name>
        <param-value>SimplePageCachingFilter</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PageCachingFilter</filter-name>
    <url-pattern>/index.jsp</url-pattern>
</filter-mapping>

    2、添加ehcache.xml

    此处的ehcache.xml需要放在src目录下,具体是否可以配置位置还未找到

<?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="true" monitoring="autodetect" dynamicConfig="true">
    <cache name="SimplePageCachingFilter" 
           maxElementsInMemory="10000" eternal="false"
           overflowToDisk="false" timeToIdleSeconds="300" 
           timeToLiveSeconds="5" (保留5秒)
           memoryStoreEvictionPolicy="LFU" />
</ehcache>

    3、通过jsp测试,5秒后时间才会变动。

<%=new Date().getTime()%>

    三、jsp+ehcache缓存局部页面

    1、在web.xml中添加

<!--ehcache 页面局部缓存 -->
<filter>
    <filter-name>PageFragmentCachingFilter</filter-name>
    <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilter</filter-class>
    <init-param>
        <param-name>cacheName</param-name>
        <param-value>SimplePageFragmentCachingFilter</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PageFragmentCachingFilter</filter-name>
    <url-pattern>/_cache.jsp</url-pattern>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

    2、在ehcache中添加

<cache name="SimplePageFragmentCachingFilter" maxElementsInMemory="10000" eternal="false"
       overflowToDisk="false" timeToIdleSeconds="300" timeToLiveSeconds="10"
       memoryStoreEvictionPolicy="LFU" />

    

转载于:https://my.oschina.net/u/2331224/blog/802732

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值