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

本文介绍了OSCache作为JSP内容、Servlet响应或任意对象的缓存解决方案的特点与配置过程。OSCache支持内存缓存及磁盘持久化,并提供集群支持等特性。文中详细展示了如何在Web应用中集成OSCache,并提供了基本的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载于: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)作缓存处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值