oscache源代碼閱讀(四) -- JSP/Servlet緩存Cach
系統(tǒng)
2411 0
oscache對于jsp/servlet的緩存是使用Filter來實現的,對應的類是 com.opensymphony.oscache.web.filter.CacheFilter,既然是Filter那么要看的自然主要有三個方 法:init、doFilter和destroy,這里#destroy()并沒有具體實現,只關注前兩個即可,首先看一下#init()方法,
????
public
?
void
?init(FilterConfig?filterConfig)?
{
????????config?
=
?filterConfig;
????????log.info(
"
OSCache:?Initializing?CacheFilter?with?filter?name?
"
?
+
?config.getFilterName());
????????
//
?此變量用于防治請求被重復的緩存
????????requestFiltered?
=
?REQUEST_FILTERED?
+
?config.getFilterName();
????????log.info(
"
Request?filter?attribute?is?
"
?
+
?requestFiltered);
????????
//
?讀取配置文件
????????Properties?props?
=
?
null
;
????????
try
?
{
????????????
//
?首先按照Filter參數指定的地方讀取配置文件,否則讀取默認位置的
????????????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只有一個實例,這里需要關注一下
????????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'.
"
);
????????????}
????????}
????????
//
?利用"計劃任務"表達式來處理超時時間
????????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配置文件的讀取)。其中的這句調用時需要關注一下的,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,當然,第一次初始化時是不會找到的
????????ServletCacheAdministrator?adm
分享到:
oscache源代碼閱讀(四) -- JSP/Servlet緩存CacheFilter
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
評論