JAVA OSCache

OSCache安装

 

  1. 解压oscache-2.4-full.
  2. 把oscache-2.4.jar放到/WEB-INF/lib下.
  3. 要确保commons-logging.jar也在环境变量中.
  4. 把/etc/oscache.properties放入/WEB-INF/classes下.
  5. 把etc/oscache.tld也放在/WEB-INF/classes下.

 

OSCache应用

 

一、JSP的应用

 

应用OSCache的标签:


1.在web.xml中:

Xml代码 复制代码
  1. <taglib>    
  2.     <taglib-uri>oscache</taglib-uri>    
  3.     <taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>    
  4. </taglib>   
<taglib> 
    <taglib-uri>oscache</taglib-uri> 
    <taglib-location>/WEB-INF/classes/oscache.tld</taglib-location> 
</taglib> 

 jsp中就可以<%@ taglib uri="oscache" prefix="os"%>这样来引用了.

 

2.直接在jsp中加入OSCache的标签库引用
<%@ taglib uri="/WEB- INF/classes/oscache.tld" prefix="os"%>

官方的标签库

<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>

这样就不用再把oscache.tld放在/WEB-INF/classes下了.

 

目前OSCache有5个标签.他们是cache, usecached, flush, addgroup, addgroups:

 

<cache></cache>

OSCache中最主要的标签.括起来的内容将根据属性的设置来缓存起来.第一次执行的时候,OSCache会把cache标签中的jsp执行并且缓存起来,以后再执行的话,他会首先判断缓存的内容是否过期,如果过期那么会从新执行并缓存.否则就直接从缓存中读取.判定过期的条件如下:

 

1.缓存的内容超过了属性time所指定的时间.

2.不符合cron设置的时间间隔.

3.如果scope指定的范围刷新的话,则认为过期了.如Session过期.

 

属性如下:
key   : 缓存的Key,可以是任何的字符,用来寻找缓存的内容用的.可以理解成HashMap中的Key.不能把2个要缓存的对象定义成一个名字,那样后一个会覆盖前一个的内容.默认情况,如果不指定Key的话,OSCache也会自动生成一个Key,规则是请求的URI+当前页面的Query String.


scope  : 缓存的范围.有2个, application和session.默认值是application.


time   : 缓存内容的时间.以秒为单位,默认是3600秒.到了指定的时间,就会刷新缓存内容.如果指定一个负数的话,意味着永远不会过期.


duration : 也是用来指定缓存内容的时间,它和time属性只能是2选1,它的特点是可以用Simple Data Format 或者是ISO-8601进行日期格式化.


cron   : 用万年历的形式指定缓存内容何时过期的.它应用的Unix的万年历形式,如("0 * * * *")


refresh  : 是个Boolean值,如果是True的话,则不管前面提到的过期检查,都刷新.默认情况是false.


mode   : 设置这项为”silent”将防止把括起来的内容输出.这在你预加载缓存内容而不愿显示给用户看到时很有用.


groups  : 可以提供一个以逗号分割的组名称.如group="A, B".这将允许你以组的名义来操作他们,分组非常有用,比如你要缓存的内容正好需要另外一个应用程序的一部分或数据,当依赖的发生了改变,正好联动的可以使很多的组过期,进而使与组发生关联的缓存内容得到更新.


language : 设置编码方式.


refreshpolicyclass:指定自定义的类来处理缓存的内容什么时候过期.这个类需要从 refreshpolicyparam com.opensymphony.oscache.web.WebEntryRefreshPolicy继承.


refreshpolicyparam : 它和上面的是联合使用的.是给refreshpolicyclass传任意的参数的.指定这项的话,就必须有refreshpolicyclass,否则就不起作用.

 

Java代码 复制代码
  1. <os:cache key="<%=myKey%>" time="1800" refresh="<%=needRefresh%>">   
  2. <!--这里是要缓存的内容-->   
  3. </os:cache>   
  4. <!--这里将myKey标识的缓存内容保持30分钟,到期自动刷新.如果needRefresh为true也会刷新(适合于更新内容的即时刷新). -->  
<os:cache key="<%=myKey%>" time="1800" refresh="<%=needRefresh%>">
<!--这里是要缓存的内容-->
</os:cache>
<!--这里将myKey标识的缓存内容保持30分钟,到期自动刷新.如果needRefresh为true也会刷新(适合于更新内容的即时刷新). -->

 

Java代码 复制代码
  1. <os:cache key="<%=myKey%>" cron="0 2 * * *" refresh="<%=needRefresh%>">   
  2. <!--这里是要缓存的内容-->   
  3. </os:cache>   
  4. <!--   
  5. 将myKey标识的缓存内容在每天的凌晨2时自动刷新.如果needRefresh为true也会刷新(适合于更新内容的即时刷新).举到了这个例子,首先这五颗星的位置代表分,小时,一个月中的天,月,一周中的天   
  6. 分: 0~59  
  7. 小时 : 0~23  
  8. 天(月) : 1~31  
  9. 月 : 1~12,用英文全称也可以.如January, April   
  10. 天(周): 0~6(0代表Sunday,1代表Monday… 6代表Saturday)   
  11. 举个例子,比如我们想让缓存的内容在4月的晚上11:45分过期,可以这样来写"45 23 * April *"  
  12. -->  
<os:cache key="<%=myKey%>" cron="0 2 * * *" refresh="<%=needRefresh%>">
<!--这里是要缓存的内容-->
</os:cache>
<!--
将myKey标识的缓存内容在每天的凌晨2时自动刷新.如果needRefresh为true也会刷新(适合于更新内容的即时刷新).举到了这个例子,首先这五颗星的位置代表分,小时,一个月中的天,月,一周中的天
分: 0~59
小时 : 0~23
天(月) : 1~31
月 : 1~12,用英文全称也可以.如January, April
天(周): 0~6(0代表Sunday,1代表Monday… 6代表Saturday)
举个例子,比如我们想让缓存的内容在4月的晚上11:45分过期,可以这样来写"45 23 * April *"
-->

 

<usecached />

需要放在cache标签中嵌套使用(一般配合try..catch使用)告诉他的上级标签是否应用缓存的译本. 则出现异常时将会替换包括上级标签在内的所有内容(提示:Missing cached content).

use="true|false" : 是否应用的标记. 默认为True.一般省略.

Java代码 复制代码
  1. <os:cache>   
  2.   ..html..   
  3.          <% try {%>   
  4.          ......html   
  5.          <%}catch (Exception e) {%>   
  6.               Inside catch: <os:usecached use="<%=isUsed%>"/> YES   
  7.          <% } %>   
  8. </os:cache>   
<os:cache>
  ..html..
         <% try {%>
         ......html
         <%}catch (Exception e) {%>
              Inside catch: <os:usecached use="<%=isUsed%>"/> YES
         <% } %>
</os:cache> 

 出现异常时的页面输出有两种:
1. use=false
..html..
......html

Inside catch: YES
2. use=true
Missing cached content

 

<flush />

 

 

 

 

Java代码 复制代码
  1. package com.shoo.test.cache;   
  2.   
  3. import java.util.Date;   
  4.   
  5. import com.opensymphony.oscache.base.NeedsRefreshException;   
  6. import com.opensymphony.oscache.general.GeneralCacheAdministrator;   
  7.   
  8. public class BaseCache extends GeneralCacheAdministrator {   
  9.     // 过期时间(单位为秒);   
  10.     private int refreshPeriod;   
  11.     // 关键字前缀字符;   
  12.     private String keyPrefix;   
  13.   
  14.     private static final long serialVersionUID = -4397192926052141162L;   
  15.   
  16.     public BaseCache(String keyPrefix, int refreshPeriod) {   
  17.         super();   
  18.         this.keyPrefix = keyPrefix;   
  19.         this.refreshPeriod = refreshPeriod;   
  20.     }   
  21.   
  22.     // 添加被缓存的对象;   
  23.     public void put(String key, Object value) {   
  24.         this.putInCache(this.keyPrefix + "_" + key, value);   
  25.     }   
  26.   
  27.     // 删除被缓存的对象;   
  28.     public void remove(String key) {   
  29.         this.flushEntry(this.keyPrefix + "_" + key);   
  30.     }   
  31.   
  32.     // 删除所有被缓存的对象;   
  33.     public void removeAll(Date date) {   
  34.         this.flushAll(date);   
  35.     }   
  36.   
  37.     public void removeAll() {   
  38.         this.flushAll();   
  39.     }   
  40.   
  41.     // 获取被缓存的对象;   
  42.     public Object get(String key) throws Exception {   
  43.         try {   
  44.             return this.getFromCache(this.keyPrefix + "_" + key, this.refreshPeriod);   
  45.         } catch (NeedsRefreshException e) {   
  46.             this.cancelUpdate(this.keyPrefix + "_" + key);   
  47.             throw e;   
  48.         }   
  49.     }   
  50.   
  51. }  
package com.shoo.test.cache;

import java.util.Date;

import com.opensymphony.oscache.base.NeedsRefreshException;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;

public class BaseCache extends GeneralCacheAdministrator {
	// 过期时间(单位为秒);
	private int refreshPeriod;
	// 关键字前缀字符;
	private String keyPrefix;

	private static final long serialVersionUID = -4397192926052141162L;

	public BaseCache(String keyPrefix, int refreshPeriod) {
		super();
		this.keyPrefix = keyPrefix;
		this.refreshPeriod = refreshPeriod;
	}

	// 添加被缓存的对象;
	public void put(String key, Object value) {
		this.putInCache(this.keyPrefix + "_" + key, value);
	}

	// 删除被缓存的对象;
	public void remove(String key) {
		this.flushEntry(this.keyPrefix + "_" + key);
	}

	// 删除所有被缓存的对象;
	public void removeAll(Date date) {
		this.flushAll(date);
	}

	public void removeAll() {
		this.flushAll();
	}

	// 获取被缓存的对象;
	public Object get(String key) throws Exception {
		try {
			return this.getFromCache(this.keyPrefix + "_" + key, this.refreshPeriod);
		} catch (NeedsRefreshException e) {
			this.cancelUpdate(this.keyPrefix + "_" + key);
			throw e;
		}
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OSCache是什么? OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存的功能。OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存解决方案。 2、OSCache的特点 (1) 缓存任何对象:你可以不受限制的缓存部分jsp页面或HTTP请求,任何java对象都可以缓存。 (2) 拥有全面的API:OSCache API允许你通过编程的方式来控制所有的OSCache特性。 (3) 永久缓存:缓存能被配置写入硬盘,因此允许在应用服务器的多次生命周期间缓存创建开销昂贵的数据。 (4) 支持集群:集群缓存数据能被单个的进行参数配置,不需要修改代码。 (5) 缓存过期:你可以有最大限度的控制缓存对象的过期,包括可插入式的刷新策略(如果默认性能不能满足需要时)。 3、OSCache的安装与配置 网上已经有一个不错的使用教程:http://blog.csdn.net/ezerg/archive/2004/10/14/135769.aspx 4、有关“用OSCache进行缓存对象”的研究 这个是我今天要说的东西。网上对于OSCache缓存Web页面很多说明和例子,但对于缓存对象方面说得不多,我就把自已写得一些东西放出来,让大家看一看是怎样缓存对象的! 我基于GeneralCacheAdministrator类来写的BaseCache类 view plaincopy to clipboardprint?package com.klstudio.cache; import java.util.Date; import com.opensymphony.oscache.base.NeedsRefreshException; import com.opensymphony.oscache.general.GeneralCacheAdministrator; public class BaseCache extends GeneralCacheAdministrator { //过期时间(单位为秒); private int refreshPeriod; //关键字前缀字符; private String keyPrefix; private static final long serialVersionUID = -4397192926052141162L; public BaseCache(String keyPrefix,int refreshPeriod){ super(); this.keyPrefix = keyPrefix; this.refreshPeriod = refreshPeriod; } //添加被缓存的对象; public void put(String key,Object value){ this.putInCache(this.keyPrefix+"_"+key,value); } //删除被缓存的对象; public void remove(String key){ this.flushEntry(this.keyPrefix+"_"+key); } //删除所有被缓存的对象; public void removeAll(Date date){ this.flushAll(date); } public void removeAll(){ this.flushAll(); } //获取被缓存的对象; public Object get(String key) throws Exception{ try{ return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod); } catch (NeedsRefreshException e) { this.cancelUpdate(this.keyPrefix+"_"+key); throw e; } } } package com.klstudio.cache;<br/><br/>import java.util.Date;<br/><br/>import com.opensymphony.oscache.base.NeedsRefreshException;<br/>import com.opensymphony.oscache.general.GeneralCacheAdministrator;<br/><br/>public class BaseCache extends GeneralCacheAdministrator {<br/> //过期时间(单位为秒);<br/> private int refreshPeriod;<br/> //关键字前缀字符;<br/> private String keyPrefix;<br/> <br/> private static final long serialVersionUID = -4397192926052141162L;<br/> <br/> public BaseCache(String keyPrefix,int refreshPeriod){<br/> super();<br/> this.keyPrefix = keyPrefix;<br/> this.refreshPeriod = refreshPeriod;<br/> }<br/> //添加被缓存的对象;<br/> public void put(String key,Object value){<br/> this.putInCache(this.keyPrefix+"_"+key,value);<br/> }<br/> //删除被缓存的对象;<br/> public void remove(String key){<br/> this.flushEntry(this.keyPrefix+"_"+key);<br/> }<br/> //删除所有被缓存的对象;<br/> public void removeAll(Date date){<br/> this.flushAll(date);<br/> }<br/> <br/> public void removeAll(){<br/> this.flushAll();<br/> }<br/> //获取被缓存的对象;<br/> public Object get(String key) throws Exception{<br/> try{<br/> return this.getFromCache(this.keyPrefix+"_"+key,this.refreshPeriod);<br/> } catch (NeedsRefreshException e) {<br/> this.cancelUpdate(this.keyPrefix+"_"+key);<br/> throw e;<br/> }<br/><br/> }<br/> <br/>}<br/><br/><br/> 通过CacheManager类来看怎样缓存对象的,这个类中所用的News只是具体功能的类,我就不贴出来了,你可以自己写一个! view plaincopy to clipboardprint?package com.klstudio; import com.klstudio.News; import com.klstudio.cache.BaseCache; public class CacheManager { private BaseCache newsCache; private static CacheManager instance; private static Object lock = new Object(); public CacheManager() { //这个根据配置文件来,初始BaseCache而已; newsCache = new BaseCache("news",1800); } public static CacheManager getInstance(){ if (instance == null){ synchronized( lock ){ if (instance == null){ instance = new CacheManager(); } } } return instance; } public void putNews(News news) { // TODO 自动生成方法存根 newsCache.put(news.getID(),news); } public void removeNews(String newsID) { // TODO 自动生成方法存根 newsCache.remove(newsID); } public News getNews(String newsID) { // TODO 自动生成方法存根 try { return (News) newsCache.get(newsID); } catch (Exception e) { // TODO 自动生成 catch 块 System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage()); News news = new News(newsID); this.putNews(news); return news; } } public void removeAllNews() { // TODO 自动生成方法存根 newsCache.removeAll(); } } package com.klstudio;<br/><br/>import com.klstudio.News;<br/>import com.klstudio.cache.BaseCache;<br/><br/>public class CacheManager {<br/> <br/> private BaseCache newsCache;<br/><br/> <br/> private static CacheManager instance;<br/> private static Object lock = new Object();<br/> <br/> public CacheManager() {<br/> //这个根据配置文件来,初始BaseCache而已;<br/> newsCache = new BaseCache("news",1800); <br/> }<br/> <br/> public static CacheManager getInstance(){<br/> if (instance == null){<br/> synchronized( lock ){<br/> if (instance == null){<br/> instance = new CacheManager();<br/> }<br/> }<br/> }<br/> return instance;<br/> }<br/><br/> public void putNews(News news) {<br/> // TODO 自动生成方法存根<br/> newsCache.put(news.getID(),news);<br/> }<br/><br/> public void removeNews(String newsID) {<br/> // TODO 自动生成方法存根<br/> newsCache.remove(newsID);<br/> }<br/><br/> public News getNews(String newsID) {<br/> // TODO 自动生成方法存根<br/> try {<br/> return (News) newsCache.get(newsID);<br/> } catch (Exception e) {<br/> // TODO 自动生成 catch 块<br/> System.out.println("getNews>>newsID["+newsID+"]>>"+e.getMessage());<br/> News news = new News(newsID);<br/> this.putNews(news);<br/> return news;<br/> }<br/> }<br/><br/> public void removeAllNews() {<br/> // TODO 自动生成方法存根<br/> newsCache.removeAll();<br/> }<br/><br/>}<br/><br/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值