DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > 经验分享 > Java组件

oscache的JSP应用

OSCache是一个基于web应用的组件,他的安装工作主要是对web应用进行配置,大概的步骤如下:

1. 下载、解压缩OSCache

请到OSCache的主页http://www.opensymphony.com/oscache/download.html下载Oscache的最新版本,作者下载的是OSCache的最新稳定版本2.0。

将下载后的。Zip文件解压缩到c:\oscache(后面的章节中将使用%OSCache_Home%来表示这个目录)目录下

2. 新建立一个web应用

3. 将主要组件%OSCache_Home%\oscache.jar放入WEB-INF\lib目录

4. commons-logging.jar、commons-collections.jar的处理

  • OSCache组件用Jakarta Commons Logging来处理日志信息,所以需要commons-logging.jar的支持,请将%OSCache_Home%\lib\core\commons-logging.jar放入classpath(通常意味着将这个文件放入WEB-INF\lib目录)  
  • 如果使用JDK1.3,请将%OSCache_Home%\lib\core\commons-collections.jar放入classpath,如果使用JDK1.4或者以上版本,则不需要了  

5. 将oscache.properties、oscache.tld放入WEB-INF\class目录

  • %OSCache_Home%\oscache.properties包含了对OSCache运行特征值的设置信息  
  • %OSCache_Home%\oscache.tld包含了OSCache提供的标签库的定义内容  

6. 修改web.xml文件

在web.xml文件中增加下面的内容,增加对OSCache提供的taglib的支持:

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

OSCache中按照缓存范围的不同分为两种不同的方式:一种是缓存JSP页面中部分或者全部内容,一种是基于整个页面文件的缓存。

4.1 JSP部分内容缓存


4.1.1 Cache-OSCache提供的缓存标签


这是OSCache提供的标签库中最重要的一个标签,包括在标签中的内容将应用缓存机制进行处理,处理的方式将取决于编程者对cache标签属性的设置。

第一次请求到达时,标签中的内容被处理并且缓存起来,当下一个请求到达时,缓存系统会检查这部分内容的缓存是否已经失效,主要是以下几项:

  • 1. 缓存时间超过了cache标签设置的time或者duration属性规定的超时时间  
  • 2. cron属性规定的时间比缓存信息的开始时间更晚  
  • 3. 标签中缓存的内容在缓存后又被重新刷新过  
  • 4. 其他缓存超期设定  

如果符合上面四项中的任何一项,被缓存的内容视为已经失效,这时被缓存的内容将被重新处理并且返回处理过后的信息,如果被缓存的内容没有失效,那么返回给用户的将是缓存中的信息。

cache标签的属性说明:

key - 标识缓存内容的关键词。在指定的作用范围内必须是唯一的。默认的key是被访问页面的URI和后面的请求字符串。

你可以在同一个页面中使用很多cache标签而不指定他的key属性,这种情况下系统使用该页面的URI和后面的请求字符串,另外再自动给这些key增加一个索引值来区分这些缓存内容。但是不推荐采用这样的方式。

scope - 缓存发生作用的范围,可以是application或者session

time - 缓存内容的时间段,单位是秒,默认是3600秒,也就是一个小时,如果设定一个负值,那么这部分被缓存的内容将永远不过期。 

duration - 指定缓存内容失效的时间,是相对time的另一个选择,可以使用简单日期格式或者符合USO-8601的日期格式。如:duration='PT5M' duration='5s'等

refresh - false 或者true。

如果refresh属性设置为true,不管其他的属性是否符合条件,这部分被缓存的内容都将被更新,这给编程者一种选择,决定什么时候必须刷新。 

mode - 如果编程者不希望被缓存的内容增加到给用户的响应中,可以设置mode属性为"silent"

其它可用的属性还包括:cron 、groups、language、refreshpolicyclass、refreshpolicyparam。

上面的这些属性可以单独使用,也可以根据需要组合使用,下面的例子将讲解这些常用属性的使用方式。

4.1.2 Cache标签实例分析:


1. 最简单的cache标签用法

使用默认的关键字来标识cache内容,超时时间是默认的3600秒

<cache:cache>
<%
//自己的JSP代码内容
%>
</cache:cache>  

2. 用自己指定的字符串标识缓存内容,并且设定作用范围为session。

<cache:cache key="foobar" scope="session">
<%
//自己的JSP代码内容
%>
</cache:cache>  

3.动态设定key值,使用自己指定的time属性设定缓存内容的超时时间,使用动态refresh值决定是否强制内容刷新。

因为OSCache使用key值来标识缓存内容,使用相同的key值将会被认为使用相同的的缓存内容,所以使用动态的key值可以自由的根据不同的角色、不同的要求决定使用不同的缓存内容。

<cache:cache key="<%= product.getId() %>" time="1800" refresh="<%= needRefresh %>">
<%
//自己的JSP代码内容
%>
</cache:cache>  

4. 设置time属性为负数使缓存内容永不过期

<cache:cache time="-1">
<%
//自己的JSP代码内容
%> 

5. 使用duration属性设置超期时间

<cache:cache  duration='PT5M'>
<%
//自己的JSP代码内容
%> 

6. 使用mode属性使被缓存的内容不加入给客户的响应中

<cache:cache  mode='silent'>
<%
//自己的JSP代码内容
%> 

4.2 用CashFilter实现页面级缓存


在OSCache组件中提供了一个CacheFilter用于实现页面级的缓存,主要用于对web应用中的某些动态页面进行缓存,尤其是那些需要生成pdf格式文件/报表、图片文件等的页面,不仅减少了数据库的交互、减少数据库服务器的压力,而且对于减少web服务器的性能消耗有很显著的效果。

这种功能的实现是通过在web.xml中进行配置来决定缓存哪一个或者一组页面,而且还可以设置缓存的相关属性,这种基于配置文件的实现方式对于J2EE来说应该是一种标准的实现方式了。

[注] 只有客户访问时返回http头信息中代码为200(也就是访问已经成功)的页面信息才能够被缓存

1. 缓存单个文件

修改web.xml,增加如下内容,确定对/testContent.jsp页面进行缓存。

<filter>
      <filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<!-对/testContent.jsp页面内容进行缓存-->
      <url-pattern>/testContent.jsp</url-pattern>
</filter-mapping> 

2. 缓存URL pattern

修改web.xml,增加如下内容,确定对*.jsp页面进行缓存。

<filter>
      <filter-name>CacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<!-对所有jsp页面内容进行缓存-->
      <url-pattern>*.jsp</url-pattern>
</filter-mapping> 

3. 自己设定缓存属性

在页面级缓存的情况下,可以通过设置CacheFilter的初始属性来决定缓存的一些特性:time属性设置缓存的时间段,默认为3600秒,可以根据自己的需要只有的设置,而scope属性设置,默认为application,可选项包括application、session

<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>600</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>
<!-对所有jsp页面内容进行缓存-->
<url-pattern>*.jsp</url-pattern>
</filter-mapping> 
 

5 配置

Configuration:配置oscache.properties 
1、cache.memory: true 或者 false。默认为true 不使用内存缓存而使用硬盘缓存是很愚蠢的事情。 
2、cache.capacity 缓存object的最大数量值。默认是不限制,cache不会移走任何缓存内容。负数被当作不限制。 
3、cache.algorithm 运算规则。为了使用规则,cache的size必须是指定的。 如果cache的size不指定的话,法则将不会限制缓存对象的大小。 如果你指定了cache的size,但不指定algorithm,那它会默认使用:com.opensymphony.oscache.base.algorithm.LRUCache 有下面三种规则: com.opensymphony.oscache.base.algorithm.LRUCache-last in first out,最迟插入的最先调用。默认值。 com.opensymphony.oscache.base.algorithm.FIFOCache -first int first out。 com.opensymphony.oscache.base.algorithm.UnlimitedCache -cache中的内容将永远不会被丢弃。 如果cache.capacity不指定值的话,它将被设为默认。 
4、cache.blocking 是否同步化。true 或者 false。一般设为true,避免读取脏数据。 
5。cache.unlimited.disk 指定硬盘缓存是否要作限制。默认值为false。false的状况下,disk cache capacity 将和cache.capacity的值相同。 
6、cache.persistence.class 指定类是被持久化的类。class必须实现PersistenceListener接口。 作为硬盘持久,可以实现com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener接口。 它把class的toString()输出的hash值作为文件的名称。如果你要把文件名易读(自己设定),DiskPersistenceListener 的父类也 能使用,但其可能有非法字符或者过长的名字。 注意:HashDiskPersistenceListener 和 DiskPersistenceListener 需要设定硬盘路径:cache.path 
7、cache.path 指定硬盘缓存的路径。目录如果不存在将被建立。同时注意oscache应该要有权限写文件系统。 cache.path=c:\\myapp\\cache or *ix: cache.path=/opt/myapp/cache 
8、cache.persistence.overflow.only (NEW! Since 2.1) 指定是否只有在内存不足的情况下才使用硬盘缓存。 默认值false。但推荐是true如果内存cache被允许的话。这个属性彻底的改变了cache的行为,使得persisted cache 和memory完全不同。 
9、cache.event.listeners 用逗号分离的class名列表。每个class必须实现以下接口之一,或者几个 CacheEntryEventListener:接收cache add/update/flush and remove事件 CacheMapAccessEventListener :接收cache访问事件。这个可以让你跟踪cache怎么工作。 默认是不配置任何class的。当然你可以使用一下的class: com.opensymphony.oscache.plugins.clustersupport.BroadcastingCacheEventListener -分布式的监听器。可以广播到局域网内的其他cache实例。 com.opensymphony.oscache.extra.CacheEntryEventListenerImpl -一个简单的监听器。在cache的生命周期中记录count of 所有entry的事件。 com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl -记录count of cache map events(cache hits,misses and state hits). 
10、cache.key This is the key that will be used by the ServletCacheAdministrator (and hence the custom tags) to store the cache object in the application and session scope. The default value when this property is not specified is "__oscache_cache". If you want to access this default value in your code, it is available as com.opensymphony.oscache.base.Const.DEFAULT_CACHE_KEY. 
11、cache.use.host.domain.in.key If your server is configured with multiple hosts, you may wish to add host name information to automatically generated cache keys. If so, set this property to true. The default value is false. 
12、Additional Properties In additon to the above basic options, any other properties that are specified in this file will still be loaded and can be made available to your event handlers. For example, the JavaGroupsBroadcastingListener supports the following additional properties: 
13、cache.cluster.multicast.ip The multicast IP to use for this cache cluster. Defaults to 231.12.21.132. 
14、cache.cluster.properties Specifies additional configuration options for the clustering. The default setting is UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;\ mcast_send_buf_size=150000;mcast_recv_buf_size=80000):\ PING(timeout=2000;num_initial_members=3):\ MERGE2(min_interval=5000;max_interval=10000):\ FD_SOCK:VERIFY_SUSPECT(timeout=1500):\ pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):\ UNICAST(timeout=300,600,1200,2400):\ pbcast.STABLE(desired_avg_gossip=20000):\ FRAG(frag_size=8096;down_thread=false;up_thread=false):\ pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号