asdtiang的博客 感谢blogjava提供的博客交流平台

s2sh整合ehcache页面部分缓存

Posted on 2011-03-12 22:25 asdtiang 阅读(686) 评论(0)  编辑  收藏

首先看web.xml配置:

增加以下配置:

<!--ehcache web page cache -->
     
<filter>
    
<filter-name>fragmentCache</filter-name>
    
<filter-class>net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilter
    
</filter-class>
     
<init-param>
      
<param-name>suppressStackTraces</param-name>
      
<param-value>false</param-value>
    
</init-param>
    
<init-param>
      
<param-name>cacheName</param-name>
      
<param-value>fragmentCache</param-value>
    
</init-param>
    
  
</filter>

    
<!--
        This is a filter chain. They are executed in the order below. Do not
        change the order.
    
-->

     
<filter-mapping>
    
<filter-name>fragmentCache</filter-name>
    
<url-pattern>/WEB-INF/pages/tour/tourDetailBody.jsp</url-pattern>
    
<dispatcher>INCLUDE</dispatcher> 
    
</filter-mapping>


注意,那个<dispatcher>INCLUDE</dispatcher>,不能少,少了缓存不能用。


配置中对应<jsp:include page="/WEB-INF/pages/tour/tourDetailBody.jsp"/>

2.4版本的servlet规范在部属描述符中新增加了一个<dispatcher>元素,这个元素有四个可能的值:即REQUEST,FORWARD,INCLUDE和ERROR,可以在一个<filter-mapping>元素中加入任意数目的<dispatcher>,使得filter将会作用于直接从客户端过来的request,通过forward过来的request,通过include过来的request和通过<error-page>过来的request。如果没有指定任何< dispatcher >元素,默认值是REQUEST。

 

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation
="../../main/config/ehcache.xsd">

    
<diskStore path="java.io.tmpdir" />

    
<defaultCache 
        
maxElementsInMemory="10" 
        eternal
="false"
        timeToIdleSeconds
="5" 
        timeToLiveSeconds
="10" 
        overflowToDisk
="true" />
    
<!--
        maxElementsInMemory="10"内存中的最大页面对象 
        eternal="false"
        timeToIdleSeconds="120" timeToIdleSeconds ,多长时间不访问该缓存,那么ehcache就会清除该缓存。 
        timeToLiveSeconds="240" timeToLiveSeconds,缓存的存活时间,从开始创建的时间算起。
        overflowToDisk="true" 是否写入硬盘
    
-->
    
<!-- Page and Page Fragment Caches -->

    
<cache name="fragmentCache" 
        maxElementsInMemory
="10" 
        eternal
="false"
        timeToIdleSeconds
="10000" 
        timeToLiveSeconds
="10000" 
        overflowToDisk
="true">
    
</cache>
</ehcache>


数据的更新问题:

和页面缓存一样的,根据配置文件中的cacheName获取Ehcache,再根据获取的key进行remove操作。

action中的问题:当页面请求发生时,会调用action方法,这时我们因为先方法,应该先查询cache中是否有缓存fragment存在,如果有,直接返回成功页面,如果没有则执行剩下的代




天苍苍,野茫茫,风吹草底见牛羊

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 80, comments - 24, trackbacks - 0, articles - 32

Copyright © asdtiang

asdtiang的博客 PaidMailz
点击广告网赚A(每天4个广告,每个0.0025美元,一个搜索广告0.03美元)