OSCache使用经验总结

OSCache使用经验总结
转自[url]http://www.blogjava.net/jackstudio/archive/2006/09/08/68569.html[/url]
OSCache的使用主要有4种:
POJO 缓存
HTTP Response 缓存
JSP Tag Library 缓存
O/R Data Access 缓存

1、POJO 缓存
这种方式的缓存直接调用OSCache的API进行,主要用于处理页面内容会根据参数动态改变,可以将参数设置为key值来保存数据:
首先,声明成员变量:

// OSCache Adminitrator instance
private static GeneralCacheAdministrator cacheAdmin = null;
其次,进行初始化:
public RingArtistAction() {
cacheAdmin = new GeneralCacheAdministrator();
}
将POJO进行缓存:
// Cache data key and refresh period
String key = sex + ":" + place;
int refreshPeriod=Constants.getIntegerValue(Constants.OSCACHE_REFRESH_PERIOD).intValue();
try {
// Get from the cache
artists = (Map) cacheAdmin.getFromCache(key, refreshPeriod);
} catch (NeedsRefreshException nre) {
try {
// Get the value (probably from the database)
int count = getArtistCount(sex, place, errors);
artists = getArtistData(sex, place, count, errors);
// Store in the cache
cacheAdmin.putInCache(key, artists);
} catch (Exception ex) {
// We have the current content if we want fail-over.
artists = (Map) nre.getCacheContent();
// It is essential that cancelUpdate is called if the
// cached content is not rebuilt
cacheAdmin.cancelUpdate(key);
ex.printStackTrace();
}
}

2、HTTP Response 缓存
这种方式的缓存用来处理整个页面的内容固定,不会根据参数动态改变:
首先在web.xml中配置CacheFilter:

<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>86400</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
</filter>

将所有需要缓存的页面加入filter-mapping:

<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

注意,只有返回状态为200(HttpServletResponse.SC_OK)的内容才会被缓存

3、JSP Tag 缓存
JSP Tag缓存主要用于缓存JSP页面的局部内容:

<cache:cache key="especialcategory" cron="* 5 * * *">
<jsp:include page="/ringcategory.do" flush="true" >
<jsp:param name="ringType" value="1"/>
</jsp:include>
</cache:cache>

4、O/R Data Access 缓存
请阅读参考资料的内容获取详情。

参考资料:
Taking the load off: OSCache helps databases cope:[url]http://www.theserverside.com/articles/article.tss?l=OSCacheHelpsDatabases[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值