OSCache安裝
?
- 解壓oscache-2.4-full.
- 把oscache-2.4.jar放到/WEB-INF/lib下.
- 要確保commons-logging.jar也在環境變量中.
- 把/etc/oscache.properties放入/WEB-INF/classes下.
- 把etc/oscache.tld也放在/WEB-INF/classes下.
?
OSCache應用
?
一、JSP的應用
?
應用OSCache的標簽:
1.在web.xml中:
- < taglib > ? ??
- ???? < taglib-uri > oscache </ taglib-uri > ? ??
- ???? < taglib-location > /WEB-INF/classes/oscache.tld </ taglib-location > ? ??
- </ 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,否則就不起作用.
?
- <os:cache?key= "<%=myKey%>" ?time= "1800" ?refresh= "<%=needRefresh%>" > ??
- <!--這里是要緩存的內容--> ??
- </os:cache> ??
- <!--這里將myKey標識的緩存內容保持 30 分鐘,到期自動刷新.如果needRefresh為 true 也會刷新(適合于更新內容的即時刷新).?-->??
<os:cache key="<%=myKey%>" time="1800" refresh="<%=needRefresh%>"> <!--這里是要緩存的內容--> </os:cache> <!--這里將myKey標識的緩存內容保持30分鐘,到期自動刷新.如果needRefresh為true也會刷新(適合于更新內容的即時刷新). -->
?
- <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?*" ??
- -->??
<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.一般省略.
- <os:cache> ??
- ??..html.. ??
- ?????????<%? try ?{%> ??
- ?????????......html ??
- ?????????<%} catch ?(Exception?e)?{%> ??
- ??????????????Inside? catch :?<os:usecached?use= "<%=isUsed%>" />?YES ??
- ?????????<%?}?%> ??
- </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 />
?
?
?
?
- 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; ??
- ????????} ??
- ????} ??
- ??
- }??
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; } } }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
