JAVA OSCache

系统 2231 0

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;
		}
	}

}

    

JAVA OSCache


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论