小试 EhCache web 用来缓存JSP页面

新闻类的网站经常会因为一篇或几篇文章每分钟产生上千的pv
以前接触到的是OScache来缓存客户端请求的jsp页面,不过当时不知是什么原因作用并不是很明显,检测服务器的log,使用IE访问了某个页面后,同一台电脑改换成FF或谷歌去访问同一个页面的时候缓存并没有生效,而是又到数据库去读取了一次数据才展示给请求用户

无意中在开源网站中看到了个 Ehcache web 便拿来试了下

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
version="2.5">
<display-name>EhCache-Web</display-name>
<filter>
<filter-name>PageCacheFilter</filter-name>
<filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>PageCacheFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

这里使用了个简单的过滤器来拦截所有的请求

ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../main/config/ehcache.xsd">
<diskStore path="java.io.tmpdir/ehcache" />
<cache name="SimplePageCachingFilter"
maxElementsInMemory="10000"
maxElementsOnDisk="1000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="5"
timeToLiveSeconds="10"
memoryStoreEvictionPolicy="LFU"
/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>

<diskStore path="java.io.tmpdir/ehcache" />
磁盘存储指向了系统临时文件夹下的ehcache文件夹
为了测试 timeToLiveSeconds 最大存活时间我只用了10秒,剩下的参数参看ehcache的文档

测试前还需要数据库,毕竟这里是为了检测缓存后jsp是否还会去数据库取数据

一个简单的index页面来读取数据库
index.jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="com.db.DB"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>测试</title>
</head>
<body>
<%
DB db = new DB();
ResultSet rs = null;
rs = db.getQuery("select * from user");
while(rs.next()){
Integer id = rs.getInt("id");
String s = rs.getString("name");
out.print(id+" : "+s+"<br>");
}
System.out.println(System.currentTimeMillis());
%>
</body>
</html>


数据库操作类,当然这里我精简了很多,看客对这部分可无视
我连接了test库 用户名密码用的时候怎么改你懂的
DB.java

package com.db;
import java.sql.*;
public class DB{
private final static String name="root";
private final static String pwd="";
protected Connection con;
private Statement stmt;
Statement myst;
public DB(){
con = null;
stmt = null;
myst = null;
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", name, pwd);
stmt = con.createStatement();
}
catch(Exception e){
System.out.println("DatabaseConnect error:" + e.toString());
}
}
public ResultSet getQuery(String queryStr){
ResultSet result = null;
try{
result = stmt.executeQuery(queryStr);
}catch(Exception ex){
System.out.println("getQuery error:" + ex.toString());
}
return result;
}
}


ehcache web 的下载地址,jar文件太大就不传了
http://sourceforge.net/projects/ehcache/files/ehcache-web/

我用到的jar
ehcache-core-2.4.4.jar
ehcache-web-2.0.3.jar
slf4j-api-1.5.11.jar
上面的 sourceforge 可下到
log4j.jar
mysql-connector-java-5.1.6-bin.jar
slf4j-log4j12.jar
这个是web启动时候报的缺少的jar 只要你做过SSH的东西这些肯定会有的


测试方法
1.启动了项目去访问index.jsp页面
2.然后迅速修改数据库的记录
3.刷新index.jsp页面

结果
在第一次刷新页面的时候控制台会打出当前时间,不论是否修改数据库数据,再次刷新控制台在10秒内都不会再出现System.out.的信息
同样测试多个浏览器的时候 IE访问了主页后 换用FF去访问的时候没有再次读取数据库数据

更多的关于 ehcache web 缓存的部分还在看 ...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值