直接测试oscache的webApp
日期:2008-02-26
首先下载oscache的包:目前是 https://oscache.dev.java.net/files/documents/629/61427/oscache-2.4.1-full.zip
解压其中有一个webapp的目录就是oscache自带的例子。
1.在eclipse新建一个web project 名字随便:Test
2.将webApp内容拷贝到webRoot下
3.在WEB-INF目录下新建一个lib目录,将oscache-2.4.1.jar放到里面
4.将classes 下的目录拷贝到src目录下
5.在解压缩的文件中找到oscache.tld拷贝到src目录下
6.修改web.xml,增加
<taglib>
<taglib-uri>http://www.opensymphony.com/oscache</taglib-uri>
<taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>
</taglib>
7.发布就可以了,访问http://localhost:8080/Test 看看效果。
看了一下例子就很容易的明白,cache标签的用法了。
还有一个很重要的例子他没有就是 key为空,如果key为空则默认为当前url,这个可以缓存同一个jsp,不同参数下的情况。
这个对与论坛或者分类的网页很有帮助。
代码如下:
<%@ page import="java.util.*" %>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
<%
String scope = "application";
if (request.getParameter("scope") != null)
{
scope = request.getParameter("scope");
}
%>
<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>
<a href="cachetesturl.jsp?url=1">url=1</a>
<a href="cachetesturl.jsp?url=2">url=2</a>
<br/>
<% Date start = new Date(); %> <b>Start Time</b>: <%= start %><p>
<hr>
<%-- Note that we have to supply a cache key otherwise the ''refresh'' parameter
causes the refreshed page to end up with a different cache key! --%>
<cache:cache
scope="application">
<b>Cache Time</b>: <%= new Date() %><br>
</cache:cache>
<hr>
<b>End Time</b>: <%= new Date() %><p>
<b>Running Time</b>: <%= (new Date()).getTime() - start.getTime() %> ms.<p>
</body>
当同一个jsp情况下,url不同时,缓存不同的内容。
推荐文章 |
