一. 简介
oscache是一个广泛采用的高性能的j2ee缓存框架,能应用于任何java应用程序的普通的缓存解决方案。
二. 特点
1. 可以缓存任何对象, 你可以不受限制的缓存部分jsp页面或http请求,任何java对象都能缓存
2. 可以永久缓存,因为缓存能随意的写入硬盘
3. 支持集群
4. 可以控制缓存对象的过期
三. 使用
1.对局部页面缓存
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<h1>测试1</h1>
没有缓存的日期:
<%=new Date()%>
<br>
<!-- 页面局部缓存,使用标签 -->
<cache:cache time="5" key="cacheDate" scope="application">
缓存局部页面,每5秒刷新缓存一次,日期: <%=new Date()%>
</cache:cache>
<br>
</body>
</html>
2.对整个页面缓存
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<!-- 页面全局缓存,在web.xml中配置 -->
<h1>测试2</h1>
缓存整个页面,每10秒刷新缓存一次,日期:<%=new Date()%>
<br>
</body>
</html>
3.缓存对象
public class CacheServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
int refreshPeriod = 10; // cache in 6 seconds
GeneralCacheAdministrator cache = new GeneralCacheAdministrator();
try {
// get data from cache
Date myDate = (Date) cache.getFromCache("myDate", refreshPeriod);
System.out.println("current date is : " + myDate);
} catch (NeedsRefreshException e) {
try {
Date myDate = new Date();
// put data into cache
cache.putInCache("myDate", myDate);
} catch (Exception ex) {
cache.cancelUpdate("myDate");
}
}
}
}
oscache.properties
#默认在内存中作缓存,如设置为false,那cache只能缓存到数据库或硬盘中
cache.memory=false
#持久化缓存类,如此类打开,则必须设置cache.path信息
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
#建立磁盘缓存
cache.path=D\:\\cache
#缓存元素个数
cache.capacity=1000
#cache.cluster 为集群设置信息。如
#cache.cluster.multicast.ip为广播IP地址
#cache.cluster.properties为集群属性
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<filter>
<filter-name>CacheFilter</filter-name>
<!-- CacheFilter只捕获Http头为200的页面请求,即只对无错误请求作缓存 -->
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<!-- 缓存内容的时间段,单位是秒,默认是3600秒 -->
<param-name>time</param-name>
<param-value>10</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
</filter>
<!-- cache all the cache.jsp file -->
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>/cache2.jsp</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>cacheServlet</servlet-name>
<servlet-class>com.zdp.cache.CacheServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cacheServlet</servlet-name>
<url-pattern>/cacheServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>cache1.jsp</welcome-file>
</welcome-file-list>
</web-app>
四. 核心API - GeneralCacheAdministrator:
1.void putInCache(String key,Object content);//put on Object in a cache
2.Object getFromCache(String key);// get on Object from the Cache
3.void removeEntry(String key);// remove an Object in a cache
4.void flushEntry(String key);// flushes a single cache entry
5.void flushAll();// flush the entire cache immediately
6.void cancelUpdate(String key);// cacels a pending cache update
参加资料:http://www.cnblogs.com/huzi007/p/3873396.html