亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

oscache源代碼閱讀(四) -- JSP/Servlet緩存Cach

系統(tǒng) 2692 0

oscache對于jsp/servlet的緩存是使用Filter來實現(xiàn)的,對應(yīng)的類是 com.opensymphony.oscache.web.filter.CacheFilter,既然是Filter那么要看的自然主要有三個方 法:init、doFilter和destroy,這里#destroy()并沒有具體實現(xiàn),只關(guān)注前兩個即可,首先看一下#init()方法,

???? public ? void ?init(FilterConfig?filterConfig)? {
????????config?
= ?filterConfig;

????????log.info(
" OSCache:?Initializing?CacheFilter?with?filter?name? " ? + ?config.getFilterName());

????????
// ?此變量用于防治請求被重復(fù)的緩存
????????requestFiltered? = ?REQUEST_FILTERED? + ?config.getFilterName();
????????log.info(
" Request?filter?attribute?is? " ? + ?requestFiltered);

????????
// ?讀取配置文件
????????Properties?props? = ? null ;
????????
try ? {
????????????
// ?首先按照Filter參數(shù)指定的地方讀取配置文件,否則讀取默認位置的
????????????String?propertiesfile? = ?config.getInitParameter( " oscache-properties-file " );

????????????
if ?(propertiesfile? != ? null ? && ?propertiesfile.length()? > ? 0 )? {
????????????????props?
= ?Config.loadProperties(propertiesfile,
????????????????????????
" CacheFilter?with?filter?name?' " ? + ?config.getFilterName()? + ? " ' " );
????????????}

????????}
? catch ?(Exception?e)? {
????????????log.info(
" OSCache:?Init?parameter?'oscache-properties-file'?not?set,?using?default. " );
????????}


????????
// ?實例化ServletCacheAdministrator,ServletCacheAdministrator只有一個實例,這里需要關(guān)注一下
????????admin? = ?ServletCacheAdministrator.getInstance(config.getServletContext(),?props);

????????
// ?緩存超時時間
????????String?timeParam? = ?config.getInitParameter( " time " );
????????
if ?(timeParam? != ? null )? {
????????????
try ? {
????????????????setTime(Integer.parseInt(timeParam));
????????????}
? catch ?(NumberFormatException?nfe)? {
????????????????log.error(
" OSCache:?Unexpected?value?for?the?init?parameter?'time',?defaulting?to?one?hour.?Message= "
????????????????????????
+ ?nfe.getMessage());
????????????}

????????}


????????
// ?緩存范圍
????????String?scopeParam? = ?config.getInitParameter( " scope " );
????????
if ?(scopeParam? != ? null )? {
????????????
if ?( " session " .equalsIgnoreCase(scopeParam))? {
????????????????setCacheScope(PageContext.SESSION_SCOPE);
????????????}
? else ? if ?( " application " .equalsIgnoreCase(scopeParam))? {
????????????????setCacheScope(PageContext.APPLICATION_SCOPE);
????????????}
? else ? {
????????????????log.error(
" OSCache:?Wrong?value?' " ? + ?scopeParam
????????????????????????
+ ? " '?for?init?parameter?'scope',?defaulting?to?'application'. " );
????????????}


????????}


????????
// ?利用"計劃任務(wù)"表達式來處理超時時間
????????setCron(config.getInitParameter( " cron " ));

????????
// ?是否處理include
????????String?fragmentParam? = ?config.getInitParameter( " fragment " );
????????
if ?(fragmentParam? != ? null )? {
????????????
if ?( " no " .equalsIgnoreCase(fragmentParam))? {
????????????????setFragment(FRAGMENT_NO);
????????????}
? else ? if ?( " yes " .equalsIgnoreCase(fragmentParam))? {
????????????????setFragment(FRAGMENT_YES);
????????????}
? else ? if ?( " auto " .equalsIgnoreCase(fragmentParam))? {
????????????????setFragment(FRAGMENT_AUTODETECT);
????????????}
? else ? {
????????????????log.error(
" OSCache:?Wrong?value?' " ? + ?fragmentParam
????????????????????????
+ ? " '?for?init?parameter?'fragment',?defaulting?to?'auto?detect'. " );
????????????}

????????}


????????
// ?是否處理URL里包括session?id的請求
????????String?nocacheParam? = ?config.getInitParameter( " nocache " );
????????
if ?(nocacheParam? != ? null )? {
????????????
if ?( " off " .equalsIgnoreCase(nocacheParam))? {
????????????????nocache?
= ?NOCACHE_OFF;
????????????}
? else ? if ?( " sessionIdInURL " .equalsIgnoreCase(nocacheParam))? {
????????????????nocache?
= ?NOCACHE_SESSION_ID_IN_URL;
????????????}
? else ? {
????????????????log.error(
" OSCache:?Wrong?value?' " ? + ?nocacheParam
????????????????????????
+ ? " '?for?init?parameter?'nocache',?defaulting?to?'off'. " );
????????????}

????????}


????????
// ?是否處理寫入到response中的header屬性Last-Modified
????????String?lastModifiedParam? = ?config.getInitParameter( " lastModified " );
????????
if ?(lastModifiedParam? != ? null )? {
????????????
if ?( " off " .equalsIgnoreCase(lastModifiedParam))? {
????????????????lastModified?
= ?LAST_MODIFIED_OFF;
????????????}
? else ? if ?( " on " .equalsIgnoreCase(lastModifiedParam))? {
????????????????lastModified?
= ?LAST_MODIFIED_ON;
????????????}
? else ? if ?( " initial " .equalsIgnoreCase(lastModifiedParam))? {
????????????????lastModified?
= ?LAST_MODIFIED_INITIAL;
????????????}
? else ? {
????????????????log.error(
" OSCache:?Wrong?value?' " ? + ?lastModifiedParam
????????????????????????
+ ? " '?for?init?parameter?'lastModified',?defaulting?to?'initial'. " );
????????????}

????????}


????????
// ?是否處理寫入到response中的header屬性Expires
????????String?expiresParam? = ?config.getInitParameter( " expires " );
????????
if ?(expiresParam? != ? null )? {
????????????
if ?( " off " .equalsIgnoreCase(expiresParam))? {
????????????????setExpires(EXPIRES_OFF);
????????????}
? else ? if ?( " on " .equalsIgnoreCase(expiresParam))? {
????????????????setExpires(EXPIRES_ON);
????????????}
? else ? if ?( " time " .equalsIgnoreCase(expiresParam))? {
????????????????setExpires(EXPIRES_TIME);
????????????}
? else ? {
????????????????log.error(
" OSCache:?Wrong?value?' " ? + ?expiresParam
????????????????????????
+ ? " '?for?init?parameter?'expires',?defaulting?to?'on'. " );
????????????}

????????}


????????
// ?是否處理寫入到response中的header屬性Cache-Control
????????String?cacheControlMaxAgeParam? = ?config.getInitParameter( " max-age " );
????????
if ?(cacheControlMaxAgeParam? != ? null )? {
????????????
if ?(cacheControlMaxAgeParam.equalsIgnoreCase( " no?init " ))? {
????????????????setCacheControlMaxAge(MAX_AGE_NO_INIT);
????????????}
? else ? if ?(cacheControlMaxAgeParam.equalsIgnoreCase( " time " ))? {
????????????????setCacheControlMaxAge(MAX_AGE_TIME);
????????????}
? else ? {
????????????????
try ? {
????????????????????setCacheControlMaxAge(Long.parseLong(cacheControlMaxAgeParam));
????????????????}
? catch ?(NumberFormatException?nfe)? {
????????????????????log.error(
" OSCache:?Unexpected?value?for?the?init?parameter?'max-age',?defaulting?to?'60'.?Message= "
????????????????????????????
+ ?nfe.getMessage());
????????????????}

????????????}

????????}


????????
// ?ICacheKeyProvider的實例,用于創(chuàng)建緩存的key
????????ICacheKeyProvider?cacheKeyProviderParam? = ?(ICacheKeyProvider)?instantiateFromInitParam(
????????????????
" ICacheKeyProvider " ,?ICacheKeyProvider. class ,? this .getClass().getName());
????????
if ?(cacheKeyProviderParam? != ? null )? {
????????????setCacheKeyProvider(cacheKeyProviderParam);
????????}


????????
// ?ICacheGroupsProvider的實例,用于創(chuàng)建緩存的group名字
????????ICacheGroupsProvider?cacheGroupsProviderParam? = ?(ICacheGroupsProvider)?instantiateFromInitParam(
????????????????
" ICacheGroupsProvider " ,?ICacheGroupsProvider. class ,? this .getClass().getName());
????????
if ?(cacheGroupsProviderParam? != ? null )? {
????????????setCacheGroupsProvider(cacheGroupsProviderParam);
????????}


????????
// ?EntryRefreshPolicy的實例,用于指定緩存過期策略
????????EntryRefreshPolicy?expiresRefreshPolicyParam? = ?(EntryRefreshPolicy)?instantiateFromInitParam(
????????????????
" EntryRefreshPolicy " ,?EntryRefreshPolicy. class ,?ExpiresRefreshPolicy. class .getName());
????????
if ?(expiresRefreshPolicyParam? != ? null )? {
????????????setExpiresRefreshPolicy(expiresRefreshPolicyParam);
????????}
? else ? {
????????????setExpiresRefreshPolicy(
new ?ExpiresRefreshPolicy(time));
????????}


????????
// ?指定哪些請求方式不去緩存,如GET,POST等
????????String?disableCacheOnMethodsParam? = ?config.getInitParameter( " disableCacheOnMethods " );
????????
if ?(StringUtil.hasLength(disableCacheOnMethodsParam))? {
????????????disableCacheOnMethods?
= ?StringUtil.split(disableCacheOnMethodsParam,? ' , ' );
????????}


????}


這個方法主要是對Filter的init-param的載入還有緩存管理器類實例的創(chuàng)建(里面會包括一個Application Scope的Cache的創(chuàng)建還有oscache配置文件的讀取)。其中的這句調(diào)用時需要關(guān)注一下的,admin = ServletCacheAdministrator.getInstance(config.getServletContext(), props),也就是ServletCacheAdministrator類實例的創(chuàng)建。

???? public ? synchronized ? static ?ServletCacheAdministrator?getInstance(ServletContext?context,?Properties?p)? {
????????
// ?Cache在ServletContext中的屬性名
????????String?adminKey? = ? null ;
????????
if ?(p? != ? null )? {
????????????
// ?這里是oscache配置文件中的cache.key屬性
????????????adminKey? = ?p.getProperty(CACHE_KEY_KEY);
????????}

????????
if ?(adminKey? == ? null )? {
????????????adminKey?
= ?DEFAULT_CACHE_KEY;
????????}

????????
// ?ServletCacheAdministrator在ServletContext中的鍵值要加上"_admin"這個后綴
????????adminKey? += ?CACHE_ADMINISTRATOR_KEY_SUFFIX;

????????
// ?先嘗試在ServletContext中找Cache,當(dāng)然,第一次初始化時是不會找到的
????????ServletCacheAdministrator?adm
2
4
分享到:
評論

oscache源代碼閱讀(四) -- JSP/Servlet緩存CacheFilter


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 伊人黄色| 中文字幕av一区 | 九月婷婷综合婷婷 | 国产在线一区二区三区 | 在线观看www成人影院 | 日本毛片在线 | 97视频免费播放观看在线视频 | 亚洲综合激情九月婷婷 | 亚洲夜夜骑 | 亚洲高清在线视频 | 久久久精品一区二区三区 | 夜夜爽66| 美女叫春| 97人人在线视频 | 国产福利免费 | 搜索一级毛片 | 福利视频欧美一区二区三区 | 毛茸茸性毛茸茸大b | 伊人影视频 | 久久久噜噜噜久久网 | 视频在线a | 亚洲综合成人在线 | 99re热视频精品首页 | 国产成人精品免费视 | 日韩在线a视频免费播放 | 国产日韩中文字幕 | 免费h片在线观看网址最新 免费v片在线观看无遮挡 | 久久亚洲精品中文字幕二区 | 亚洲啪啪免费视频 | 午夜视频国产 | 免费亚洲一区 | 91久久在线 | 超级乱淫视频播放日韩 | 国产精品视频久久久久久 | 九色国产在视频线精品视频 | 动漫美女h片黄动漫在线观看 | 94久久国产乱子伦精品免费 | 欧美日韩成人 | 欧美精品亚洲网站 | 欧美久久精品一级c片片 | 毛片免费全部播放一级 |