Posted on 2011-04-10 23:21
penngo 阅读(2609)
评论(0) 编辑 收藏 所属分类:
Java
OSCache支持JSP标签页面内缓存、整个页面的缓存、对象缓存,试用了下OSCache的页面缓存,并使用JMeter测试。
1、使用的测试页面代码test1.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Filter Test Page</title>
<style type="text/css">
body {font-family: Arial, Verdana, Geneva, Helvetica, sans-serif}
</style>
</head>
<body>
<a href="/OSCache/">返回</a><p>
<hr>
<b>当前时间</b>: 2011-04-09 12:20:07<br>
<b>当前毫秒数</b>: 1302322807867<br>
</body>
</html>
JMeter线程组设置:
运行结果:
2、把文件放在cache/test1.jsp
并在web.xml增加配置:
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>/cache/*</url-pattern>
</filter-mapping>
JMeter测试结果
3、把文件放在WEB-INF/template/test1.jsp,并写一个servlet处理文件先把test1.jsp读取进内存HashMap,访问时从这个HashMap中取出test1.jsp的文件内容。
增加一个servlet文件CacheServlet.java
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class CacheServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Map<String, byte[]> cacheMap = new HashMap<String, byte[]>();
protected static final Log log = LogFactory.getLog(CacheServlet.class);
public CacheServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
cache(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
cache(request, response);
}
protected void cache(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String path = request.getSession().getServletContext().getRealPath("")
+ "/WEB-INF/template";
String key = request.getRequestURI(); //从静态变量中取出页面
byte[] by = cacheMap.get(key);
if (by != null) { //如果不为空,则直接把文件内容返回给客户端
byte[] b = new byte[256];
int len = 0;
ServletOutputStream out = response.getOutputStream();
out.write(by);
log.debug("输出缓存==============\n" );
out.flush();
out.close();
}
else{ //如果为空,则先把文件内容以字节形式保存在静态变量中,
File f = new File(path + "/test1.jsp");
StringBuffer sr = new StringBuffer();
InputStream in = new FileInputStream(f);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = 0;
byte[] b = new byte[256];
while((len = in.read(b)) > 0){
bos.write(b, 0, len);
}
byte[] sByte = bos.toByteArray();
log.debug("生成缓存:" );
cacheMap.put(key, sByte);
in.close();
bos.close();
ServletOutputStream out = response.getOutputStream();
out.write(sByte);
out.flush();
out.close();
}
}
}
web.xml中增加内容:
<servlet>
<servlet-name>CacheServlet</servlet-name>
<servlet-class>com.ocache.servlet.CacheServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CacheServlet</servlet-name>
<url-pattern>/CacheServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
先用浏览器访问该sevlet,把页面内容保存进cacheMap中,再用JMeter测试。
测试结果:
测试发现,服务器本身对静态内容的处理比缓存还快。而自己写的servlet缓存比OSCache快,主要是因为OSCache多了内部处理的代码,至于是哈内部处理,还要看过源码才知。
本程序的测试环境:
服务器:apache-tomcat-6.0.20
缓存组件:oscache-2.4.1
JDK:jdk1.6.0_17
JMeter:jakarta-jmeter-2.4
测试代码:
OSCache.zip