1 OSCache 的基本安装
1)首先去开源中国查看一下资源介绍,然后下载对应资源,具体地址:
http://www.oschina.net/p/oscache
2)下载后(目前版本为2.4),解压得到如下目录:
3)新建一个web项目,在项目中引入上面lib中提到的jar包
commons-logging.jar 和 oscache-2.4.jar文件
4)在src目录下粘贴上面etc的两个文件
oscache.properties 和 oscache.tld
5)为项目引入标签库,有三种方式,其实也就是jsp标签引入的三种方式,分别如下:
A:只要在JSP页面中添加如下语句: <%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %> |
||||
B:只要在JSP页面中添加如下语句(直接引用): <%@ taglib uri="/WEB-INF/classes/oscache.tld" prefix="cache"%> |
||||
C:需要配置web.xml,同时在JSP页面添加(间接引用) web.xml (如果是2.4以下的可以不需要jsp-config包裹)
?
JSP页面中
?
|
6)配置页面缓存(主要是对一些图片、报表进行缓存),web.xml 中配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<listener>
<listener-class>com.opensymphony.oscache.web.CacheContextListener</listener-class>
</listener>
<session-config>
<session-timeout>3600</session-timeout>
</session-config>
<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>10</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>session</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
|
2 OSCache 的基本使用
1)oscache.properties 配置内容介绍
cache.memory=true cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener cache.path=D:\\oscache cache.capacity=1000 cache.unlimited.disk=false |
1)是否使用内存缓存,默认为true 2)如果使用磁盘缓存,这个属性必须有值,否则采用内存缓存 3)磁盘缓存的位置,这个属性需要看系统设置路径 4)缓存容量大小 5)磁盘缓存大小是否限定 |
2)JSP页面缓存,主要就是通过如下标签
|
具体介绍: 1)cache用来缓存某一个具体的内容,那么保证其属性key的值唯一性,同时指定一个超时时间,告诉系统缓存什么时间段有效! 2)usecached用来作为一种保险方式,当获取当前缓存出错时,可以采用系统以前旧版本的缓存,这个标签必须在cache标签内。 3)flush用来强制清空缓存,缓存有应用范围分为3个,application、session、null;如果为null,则会清除前两者的缓存,否则只清除指定范围内的缓存。 4)addgrop 与 addgroups 必须在cache标签内使用,他们是意义在于统一控制管理,换句话,如果谁(比如 flush)引用了groupID,对其做了限制,那么这个缓存将会受到他的影响。同时,他也受控于自身所属的cache。 |
3)cache标签的重要属性
cache | 1)key :这个缓存的键,需要唯一标识 2)scope:应用范围,application、session、null 3)time:缓存有效时间,默认3600秒 4)duration:缓存有效期时间,这个时间可以采用简单的格式 5)cron:通过cron表达式设置缓存时间,这个可以参考文档 6)refresh:如果为true,则缓存内容得到刷新,也就是从新缓存内容 7)groups:为缓存指定一个分组,便于统一管理 |
3 页面缓存
页面缓存只要配置在web.xml 中配置对应的URL-pattern格式,就可以顺利进行页面缓存,具体上面第六步安装配置。
4 API使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
// 获取缓存管理对象,这个对象最好做一个单例
ServletCacheAdministrator admin = ServletCacheAdministrator.getInstance(getServletContext());
// 一般对于页面缓存,经常使用某个特定参数的值作为缓存的key
String key = req.getParameter("key");
// 缓存的值
String myValue = "";
// 缓存有效期
int myRefreshPeriod = 1000;
// 是否flush缓存
boolean updated = false;
// 缓存内容操作
try {
// 从指定应用范围中获取缓存内容
myValue = (String) admin.getFromCache(PageContext.SESSION_SCOPE,req,key,myRefreshPeriod);
} catch (NeedsRefreshException nre) {
try {
// 如果取值发生错误,那么可以进行手动添加缓存
myValue = "This is the content retrieved.";
// 添加缓存
admin.putInCache(PageContext.SESSION_SCOPE,req,key,myValue);
updated = true;
} finally {
if (!updated) {
// It is essential that cancelUpdate is called if the
// cached content could not be rebuilt
admin.cancelUpdate(PageContext.SESSION_SCOPE,req,key);
}
}
}
|