简述
高性能的J2EE缓存框架,主要是对页面的缓存,可以整页或者指定网页某一部分缓存,同时指定他的过期时间。
特点:能永久缓存写入磁盘、缓存任何Java对象 JSP页面 HTTP请求、支持集群、控制缓存过期时间。
使用
oscache 下载地址 http://www.opensymphony.com/oscache/download.action
需要的jar包有:oscache.jar、commons-logging.jar
需要将oscache.properties放在src下面.
全局缓存
<!-- 设置页面的全局缓存 -->
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>7200</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>/productst.do</url-pattern>
</filter-mapping>
局部缓存
<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="oscache" %>
<!--缓存默认存放在application范围,缓存时间默认为3600秒,即1小时。-->
<body>
<oscache:cache key="huhui" scope="session" time="15" refresh="${param.refresh }">
<!-- 是使用Map对象来存储缓存的,默认的key是uri路径,如:/oscache/index.jsp,也可以指定它的key -->
<div><%=new Date() %></div>
</oscache:cache>
当前时间:<%=new Date() %>
</body>
内存缓存/硬盘缓存(推荐使用内存缓存,比硬盘缓存要快得多)oscache.properties
#指定是否使用内存缓存,默认值为true,即使用内存缓存
cache.memory=true
#指定缓存的容量,默认的容量是无限的
cache.capacity=30000
#如果要使用硬盘缓存,可以这样设置:
cache.memory=false
#指定缓存保存的路径
cache.path=E:\\oscache
#用于设置持久化的类
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
清除缓存
<oscache:flush scope="application"/> 清除application范围内的所有缓存
<oscache:flush scope="session" key="huhui"/> 清除session范围内的key为huhui的缓存
<oscache:flush scope="application" group="hu"/> 清除application范围内组名为hu内的所有缓存