新闻类的网站经常会因为一篇或几篇文章每分钟产生上千的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 缓存的部分还在看 ...
分享到:
相关推荐
ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache页面缓存技术ehcache...
springMVC+Ehcache的各级缓存(包括页面缓存)
基于java的开发源码-ehcache(Java缓存框架 EhCache).zip 基于java的开发源码-ehcache(Java缓存框架 EhCache).zip 基于java的开发源码-ehcache(Java缓存框架 EhCache).zip 基于java的开发源码-ehcache(Java缓存框架 ...
ehcache缓存页面 ehcache缓存页面
EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。Ehcache 是一种广泛使用的开源 Java 分布式缓存。主要面向通用缓存,Java EE 和轻量级容器。它具有内存和...
2、由于要实现Ehcache缓存页面,所以必须要添加“ehcache-web-2.0.4.jar” jar包,该jar包主要用于辅助Ehcache实现页面缓存 注意: 本web工程的发布不要使用Tomcat7,否则会出现如下异常: 2015-3-25 9:53:50 org....
ehcache二级缓存配置文件
Ehcache 整合Spring 使用页面、对象缓存
以java配置类方式 整合EhCache来实现页面整体缓存及页面局部缓存。详情见博客http://blog.csdn.net/poorCoder_/article/details/56483954
配置 EhCache 的实例 1、 简介: EhCache缓存插件是Hibernate的另一个项目,Hibernate框架内置了对它 的支持,对于单机应用推荐使用它做为Hibernate的二级缓存。
ehcache.jar 缓存jar
该压缩包用于缓存服务部署,jboss集成ehcache,解压启动即可访问,实现缓存共用
Hibernate+ehcache二级缓存技术 Hibernate+ehcache二级缓存技术
ehcache项目缓存技术 ,大量用户需要访问主页的解决方案,缓存解决频繁操作某些数据的时候可能用到
ehcache(Java缓存框架 EhCache)
java源码:ehcache(Java缓存框架 EhCache).rar
Hibernate EhCache 二级缓存配置
NULL 博文链接:https://wiselyman.iteye.com/blog/2114715
java ehcache分布式缓存实例 RMI方式实现.