您的位置:首页 > 编程语言 > Java开发

了解一下java系统中的OSCache配置

2012-02-21 18:24 615 查看
转载于:http://hi.baidu.com/wader2006/blog/item/0965a18bdc3d117e9f2fb435.html

最近一朋友说到系统的架构是Velocity+Hibernate,中间控制层是用servlet,其中用到了OSCache作为缓存系统!

现在我们来先了解一下OSChache的功能:

OSCache is a caching solution that includes a JSP tag library and set of classes to perform fine grained dynamic caching of JSP content, servlet responses or arbitrary objects. It provides both in memory and persistent on disk
caches, and can allow your site to have graceful error tolerance (eg if an error occurs like your db goes down, you can serve the cached content so people can still surf the site almost without knowing). Take a look at the great
features of OSCache.

features:

Fast in-memory caching/Persistent on-disk caching/Excellent Performance

Clustering support/Flexible Caching System/Simple JSP Tag Library

Caching Filter/Comprehensive API/Exception Handling/Cache Flushing

Portable caching/i18n Aware/Solid Reputation

现在来看看OSCache的配置过程:

http://www.opensymphony.com/oscache下载合适的OSCache版本;(oscache-2.3.2-full)

从解压缩目录取得oscache.jar 文件放到 /WEB-INF/lib 或相应类库目录中;

如果你的jdk版本为1.3.x,建议在lib中加入Apache Common Lib 的commons-collections.jar包,jdk是1.4以上则不必;

从src或etc目录取得oscache.properties 文件,放入src根目录或发布环境的/WEB-INF/classes 目录;

如你需要建立磁盘缓存,须修改oscache.properties 中的cache.path信息 (去掉前面的#注释);

拷贝OSCache标签库文件oscache.tld到/WEB-INF/classes目录;

web.xml加入下列代码

<taglib>

<taglib-uri>oscache</taglib-uri>

<taglib-location>/WEB-INF/classes/oscache.tld</taglib-location>

</taglib>

oscache.properties 文件配置:

cache.memory:true 或 false ,默认为在内存中作缓存;

cache.capacity :缓存元素个数;

cache.persistence.class:持久化缓存类,如此类打开,则必须设置cache.path信息;

cache.cluster:集群设置信息;

cache.cluster.multicast.ip为广播IP地址;

cache.cluster.properties为集群属性;

OSCache的基本用法:(略)

======================cache1.jsp 内容如下=================

<%@ page contentType="text/html;charset=GBK"%>

<%@ page import="java.util.*" %>

<%@ taglib uri="oscache" prefix="cache" %>

<html>

<body>

没有缓存的日期: <%= new Date() %><p>

<!--自动刷新-->

<cache:cache time="30">

每30秒刷新缓存一次的日期: <%= new Date() %> <p>

</cache:cache>

<!--手动刷新-->

<cache:cache key="testcache">

手动刷新缓存的日期: <%= new Date() %> <p>

</cache:cache>

<a href="cache2.jsp">手动刷新</a>

</body>

</html>

=========================================================

====================cache2.jsp 执行手动刷新页面如下===========

<%@ page contentType="text/html;charset=GBK"%>

<%@ taglib uri="oscache" prefix="cache" %>

<html>

<body>缓存已刷新...<p>

<cache:flush key="testcache" scope="application"/>

<a href="cache1.jsp">返回</a>

</body>

</html>

==========================================================

缓存过滤器 CacheFilter:

在web.xml中定义缓存过滤器;参数如下:

<filter>

<filter-name>CacheFilter </filter-name>

<display-name>CacheFilter </display-name>

<description>CacheFilter </description>

<filter-class>com.opensymphony.oscache.web.filter.CacheFilter </filter-class>

<init-param>

<param-name>time </param-name>

<param-value>60 </param-value>

</init-param>

<init-param>

<param-name>scope </param-name>

<param-value>session </param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>CacheFilter </filter-name>

<url-pattern>*.jsp</url-pattern>

</filter-mapping>

定义将缓存所有.jsp页面,缓存刷新时间为60秒,缓存作用域为Session,注意,CacheFilter只捕获Http头为200的页面请求,即只对无错误请求作缓存;而不对其他请求(如500,404,400)作缓存处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: