oscache的简单配置与使用
准备工作:
地址:https://java.net/downloads/oscache/ 本人使用2.4版本
解压后的目录:
1.搭建动态web项目,
把 oscache-2.4.jar拷贝到WEB_INF下的lib目录下
把etc目录里面的2个文件oscache.properties和oscache.tld拷贝到项目的src目录下
2.修改oscache.properties文件
#cache.memory = false 去除# 改false为true
cache.memory = true
开启内存缓存
使用:
案例一:
1先向jsp文件中引入标签 <%@taglib uri="http://www.opensymphony.com/oscache" prefix="oscache" %>
2.使用oscache:cache 标签去包裹去要缓存的内容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.opensymphony.com/oscache" prefix="oscache"%>
<!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>
现在时间:<%= new Date() %> <br />
<oscache:cache time="3">
缓存时间:<%= new Date() %>
</oscache:cache>
案例二:
jsp页面不需要任何变动(jsp页面不用引入oscache标签),直接在web.xml中添加对jsp文件的过滤并进行缓存,具体配置如下:
<filter>
<filter-name>oscache</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>5</param-value> // 时间单位是妙
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>oscache</filter-name>
<url-pattern>*.jsp</url-pattern> //对所所有jsp页面有效
</filter-mapping>
说明:
jsp中<%@taglib uri="http://www.opensymphony.com/oscache" prefix="oscache"%>
的等价于写法
<%@taglib uri="cache" prefix="oscache"%>
并在web.xml中添加如下配置
<jsp-config>
<taglib>
<taglib-uri>cache</taglib-uri>
<taglib-location>classpath:oscache.tld</taglib-location>
</taglib>
</jsp-config>
前者是直接网络上去找,后者是使用本机离线文件
其他:
osCache常用标签例如
<oscache:cache time="30" >
//自上一次刷新后开始计时,过了30秒后刷新页面.页面内容才会更新.
<oscache:cache>
<cache:flush refresh="${param.fre}" scope="application"/>
<a href="cache1.jsp">返回</a>
</body>
支持el表达式可从param和session域中取值