第3篇-OsCache页面缓存

一、OSCache提供的缓存标签
这是OSCache提供的标签库中最重要的一个标签,包括在标签中的内容将应用缓存机制进行处理,处理的方式将取决于编程者对cache标签属性的设置。

第一次请求到达时,标签中的内容被处理并且缓存起来,当下一个请求到达时,缓存系统会检查这部分内容的缓存是否已经失效,主要是以下几项:
1. 缓存时间超过了cache标签设置的time或者duration属性规定的超时时间;
2. cron属性规定的时间比缓存信息的开始时间更晚;
3. 标签中缓存的内容在缓存后又被重新刷新过;
4. 其他缓存超期设定。

如果符合上面四项中的任何一项,被缓存的内容视为已经失效,这时被缓存的内容将被重新处理并且返回处理过后的信息,如果被缓存的内容没有失效,那么返回给用户的将是缓存中的信息。

cache标签的属性说明:
key - 标识缓存内容的关键词。在指定的作用范围内必须是唯一的。默认的key是被访问页面的URI和后面的请求字符串。
你可以在同一个页面中使用很多cache标签而不指定他的key属性,这种情况下系统使用该页面的URI和后面的请求字符串,另外再自动给这些key增加一个索引值来区分这些缓存内容。但是不推荐采用这样的方式。

scope - 缓存发生作用的范围,可以是application或者session。

time - 缓存内容的时间段,单位是秒,默认是3600秒,也就是一个小时,如果设定一个负值,那么这部分被缓存的内容将永远不过期。

duration - 指定缓存内容失效的时间,是相对time的另一个选择,可以使用简单日期格式或者符合USO-8601的日期格式。

refresh - false 或者true。
如果refresh属性设置为true,不管其他的属性是否符合条件,这部分被缓存的内容都将被更新,这给编程者一种选择,决定什么时候必须刷新。

mode - 如果编程者不希望被缓存的内容增加到给用户的响应中,可以设置mode属性为"silent"。
其它可用的属性还包括:cron 、groups、language、refreshpolicyclass、refreshpolicyparam。
上面的这些属性可以单独使用,也可以根据需要组合使用,下面的例子将讲解这些常用属性的使用方式。

二、Cache标签实例分析
1. 最简单的cache标签用法
使用默认的关键字来标识cache内容,超时时间是默认的3600秒
<cache:cache>
<% //自己的JSP代码内容 %>
</cache:cache>

2. 用自己指定的字符串标识缓存内容,并且设定作用范围为session。
<cache:cache key="foobar" scope="session">
<% //自己的JSP代码内容 %>
</cache:cache>

3.动态设定key值,使用自己指定的time属性设定缓存内容的超时时间,使用动态refresh值决定是否强制内容刷新。
因为OSCache使用key值来标识缓存内容,使用相同的key值将会被认为使用相同的的缓存内容,所以使用动态的key值可以自由的根据不同的角色、不同的要求决定使用不同的缓存内容。
<cache:cache key="<%= product.getId() %>" time="1800" refresh="<%= needRefresh %>">
<% //自己的JSP代码内容 %>
</cache:cache>

4. 设置time属性为负数使缓存内容永不过期
<cache:cache time="-1">
<% //自己的JSP代码内容 %>
</cache:cache>

5. 使用duration属性设置超期时间
<cache:cache duration=''PT5M''>
<% //自己的JSP代码内容 %>
</cache:cache>

6. 使用mode属性使被缓存的内容不加入给客户的响应中
<cache:cache mode=''silent''>
<% //自己的JSP代码内容 %>
</cache:cache>
案例一
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache"%>
<html>
    <body>
            没有缓存的日期: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><p>
            <!--自动刷新-->
            <cache:cache time="30">
                每30秒刷新缓存一次的日期: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><p>
            </cache:cache>
            <!--手动刷新-->
            <cache:cache key="testcache">
                手动刷新缓存的日期: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><p>
            </cache:cache>
            <a href="cache2.jsp">手动刷新</a>
    </body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache"%>

<html>
    <body>
        缓存已刷新... 
        <cache:flush key="testcache" scope="application" />
        <a href="cache1.jsp">返回</a>
    </body>
</html>


三、缓存过滤器 CacheFilter
你可以在web.xml中定义缓存过滤器,定义特定资源的缓存。
<filter>
    <filter-name>CacheFilter</filter-name>
        <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
    <init-param>
        <param-name>time</param-name>
        <param-value>60</param-value>
    </init-param>
    <init-param>
        <param-name>scope</param-name>
        <param-value>session</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CacheFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
</filter-mapping>


上面定义将缓存所有.jsp页面,缓存刷新时间为60秒,缓存作用域为Session

注意,CacheFilter只捕获Http头为200的页面请求,即只对无错误请求作缓存,而不对其他请求(如500,404,400)作缓存处理。

如果在jsp中使用如下标签
<cache:cache key="foobar" scope="session"> 
      some jsp content 
</cache:cache>


那么这中间的一段jsp代码将会以key="foobar"缓存在session中,任何其他页面中使用这个key的cache标签都能共享这段存在缓存中的执行结果。

考虑一个需求,一个页面是有许多个不同的jsp文件拼出来的,可能在页首有随机的广告,登录用户的信息,系统的即时信息,固定的目录信息等等;这其中可以考虑将固定的目录信息放入缓存中,而其他动态信息则即时刷新;再进一步考虑有时候页面之间的信息是关联的,只有当其中一条信息的内容变化了才需要去刷新。

对于这种需求就可以考虑在<cache:cache/>标签中配置group属性,将不同的具有关联关系的cache内容分组,这样oscache会自动的帮你检查该组缓存内容的变化情况,如果有任何一子成员组的内容变化了则会执行刷新,这样就可以在页面实现数据的动态同步。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache"%>

<head>
    <title>Test Page</title>
    <style type="text/css">
body {
    font-family: Arial, Verdana, Geneva, Helvetica, sans-serif
}
</style>
</head>
<body>

    <a href="<%=request.getContextPath()%>/">Back to index</a>
    <p>
    <hr>
    Flushing 'group2'
    <hr>
    <cache:flush group='group2' scope='application' />
    <hr>
    <!-- 这里有两个cache分组group1和group2,将group2设置为每次都执行刷新,所以test1为key的cache每次刷新页面内容都是重新执行过的 -->
    <cache:cache key='test1' groups='group1,group2' duration='5s'>
        <b>Cache Time1</b>: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><br>  
         This is some cache content test1 that is in 'group1' and 'group2'. Normally it would refresh if it  
         was more than 5 seconds old, however the 
         <cache:flush group='group2' scope='application' />  
         tag causes this entry to be flushed on every page refresh.<br>
    </cache:cache>
    <hr>
    
    <!-- test2只有当间隔时间超过5秒才会更新内容 -->
    <cache:cache key='test2' groups='group1' duration='5s'>
         <b>Cache Time2</b>: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><br>  
         This is some cache content test2 that is in 'group1' (refreshes if more than 5 seconds old)<br>
    </cache:cache>
    <hr>

    <!--每隔20秒刷新一次->
    <cache:cache key='test3' duration='20s'>
        <b>Cache Time3</b>: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><br>  
         This is some cache content test3 that is in 'group1' and 'group2'. The groups are added using the  tag.<br>
    </cache:cache>
    <hr>
     
    <!--实时刷新-->
    <cache:cache key='test4' duration='20s'>
        <b>Cache Time4</b>: <%=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())%><br>  
        This is some cache content test4 that is in 'group1' and 'group2'. The groups are added using the tag.<br>
        <cache:addgroups groups='group1,group2' />
    </cache:cache>
    <hr>
</body>
</html>

<cache:addgroup group='{you_group}'/>可以将所在的you_group加入当前所在位置的group中

猜你喜欢

转载自liu-wh.iteye.com/blog/2195299