打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
osache讲解2

在目前流行的三种开源的缓存工具中,OSCache的配置和使用应给是最简单的了,它主要是针对页面级的配置,EHCache主要针对对象级的缓存,MemCached应该是比较完整的了。接下来我们简单的讲一下在你的系统中怎样快速的应用上OSCache。只需简单的两步。

第一步:加载oscache.properties文件,默认放到src目录下。下面是oscache.xml的默认配置。你只需简单的去掉一些注释就可以用了。

# CACHE IN MEMORY
#
# If you want to disable memory caching, just uncomment this line.
#
# cache.memory=false


# CACHE KEY
#
# This is the key that will be used to store the cache in the application
# and session scope.
#
# If you want to set the cache key to anything other than the default
# uncomment this line and change the cache.key
#
# cache.key=__oscache_cache


# USE HOST DOMAIN NAME IN KEY
#
# Servers for multiple host domains may wish to add host name info to
# the generation of the key. If this is true, then uncomment the
# following line.
#
# cache.use.host.domain.in.key=true


# CACHE LISTENERS
#
# These hook OSCache events and perform various actions such as logging
# cache hits and misses, or broadcasting to other cache instances across a cluster.
# See the documentation for further information.
#
# cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener, \
# com.opensymphony.oscache.extra.CacheEntryEventListenerImpl, \
# com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl, \
# com.opensymphony.oscache.extra.ScopeEventListenerImpl, \
# com.opensymphony.oscache.extra.StatisticListenerImpl


# CACHE PERSISTENCE CLASS
#
# Specify the class to use for persistence. If you use the supplied DiskPersistenceListener,
# don't forget to supply the cache.path property to specify the location of the cache
# directory.

# If a persistence class is not specified, OSCache will use memory caching only.
#
# cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
# cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener

# CACHE OVERFLOW PERSISTENCE
# Use persistent cache in overflow or not. The default value is false, which means
# the persistent cache will be used at all times for every entry. true is the recommended setting.
#
# cache.persistence.overflow.only=true

# CACHE DIRECTORY
#
# This is the directory on disk where caches will be stored by the DiskPersistenceListener.
# it will be created if it doesn't already exist. Remember that OSCache must have
# write permission to this directory.
#
# Note: for Windows machines, this needs \ to be escaped
# ie Windows:
# cache.path=c:\\myapp\\cache
# or *ix:
# cache.path=/opt/myapp/cache
#
# cache.path=c:\\app\\cache


# CACHE ALGORITHM
#
# Default cache algorithm to use. Note that in order to use an algorithm
# the cache size must also be specified. If the cache size is not specified,
# the cache algorithm will be Unlimited cache.
#
# cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache
# cache.algorithm=com.opensymphony.oscache.base.algorithm.FIFOCache
# cache.algorithm=com.opensymphony.oscache.base.algorithm.UnlimitedCache

# THREAD BLOCKING BEHAVIOR
#
# When a request is made for a stale cache entry, it is possible that another thread is already
# in the process of rebuilding that entry. This setting specifies how OSCache handles the
# subsequent 'non-building' threads. The default behaviour (cache.blocking=false) is to serve
# the old content to subsequent threads until the cache entry has been updated. This provides
# the best performance (at the cost of serving slightly stale data). When blocking is enabled,
# threads will instead block until the new cache entry is ready to be served. Once the new entry
# is put in the cache the blocked threads will be restarted and given the new entry.
# Note that even if blocking is disabled, when there is no stale data available to be served
# threads will block until the data is added to the cache by the thread that is responsible
# for building the data.
#
# cache.blocking=false

# CACHE SIZE
#
# Default cache size in number of items. If a size is specified but not
# an algorithm, the cache algorithm used will be LRUCache.
#
cache.capacity=1000


# CACHE UNLIMITED DISK
# Use unlimited disk cache or not. The default value is false, which means
# the disk cache will be limited in size to the value specified by cache.capacity.
#
# cache.unlimited.disk=false


# JMS CLUSTER PROPERTIES
#
# Configuration properties for JMS clustering. See the clustering documentation
# for more information on these settings.
#
#cache.cluster.jms.topic.factory=java:comp/env/jms/TopicConnectionFactory
#cache.cluster.jms.topic.name=java:comp/env/jms/OSCacheTopic
#cache.cluster.jms.node.name=node1


# JAVAGROUPS CLUSTER PROPERTIES
#
# Configuration properites for the JavaGroups clustering. Only one of these
# should be specified. Default values (as shown below) will be used if niether
# property is set. See the clustering documentation and the JavaGroups project
# ([url]www.javagroups.com[/url]) for more information on these settings.
#
#cache.cluster.properties=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)
#cache.cluster.multicast.ip=231.12.21.132

第二步:在web.xml进行一些简单的配置,oscache提供了强大的标签。主要是针对jsp的,如果项目中需把oscache.tld放到WEB-INF/classes下,在再web.xml中配置一下。

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

在jsp中应用的话,还需简单的引入。<%@ taglib uri="oscache" prefix="cache" %>

具体的应用:<cache:cache key="testcache"></cache:cache>。

以上是针对具体的页面的配置步骤。如果想针对某一页面的类型进行配置,可以利用过滤器来配置。比如我们想对以*.ftl和*.jsp的页面进行缓存的话,我们只需这样简单的配置在web.xml中。

<!-- OScacheFilter -->
<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>3600</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*.ftl</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!-- OScacheFilter -->

如果想对某一访问的路径action进行缓存的话,我们可以这样做,在一个jsp文件中简单的加上:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="oscache" prefix="cache" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<oscache:cache>
<jsp:include page ="/view.do"/>
</oscache:cache>
</body>
</html>

OSCache的配置比较活的,你可以根据你的情况进行相应的配置。它主要是针对页面级的对象。虽然它专门为jsp提供了强大的标签支持,并不意味着不能对ftl,php进行缓存的,我们可以用jsp中的标签来嵌套我们的返回不同的数据页面。比如上面的/view.do可以返回的是一个ftl页面,照样可以缓存。

要想对缓存有更深入的了解,可以查看源码,可能会帮助你更好的理解它的工作机制和原理。

简单的说,缓存就是Map<key,value>,创建缓存就是添加一个map,使用就是通过key取value.

写的有不当之处,敬请提出,以待改正。

一、OSCache是当前运用最广的缓存方案,JBoss,Hibernate,Spring等都对其有支持,下面简单介绍一下OSCache的配置和使用过程。

http://www.opensymphony.com/oscache/download.action下载你所需要的oscache版本

本人使用的版本是oscache-2.4,以下将以此版本作为例子

1.解压缩下载的文件到指定目录
从src或etc目录取得oscache.properties 文件,放入src根目录或发布环境的/WEB-INF/classes 目录
如你需要建立磁盘缓存,须修改oscache.properties 中的cache.path信息 (去掉前面的#注释)。

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

现在你的应用目录类似如下:
$WEB_APPLICATIONWEB-INF\lib\oscache.jar
$WEB_APPLICATIONWEB-INF\classes\oscache.properties
$WEB_APPLICATIONWEB-INF\classes\oscache.tld


2.oscache.properties 文件配置向导 

cache.memory
值为true 或 false ,默认为在内存中作缓存,
如设置为false,那cache只能缓存到数据库或硬盘中,那cache还有什么意义:)

cache.capacity
缓存元素个数

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

cache.cluster 相关
为集群设置信息。

cache.cluster.multicast.ip为广播IP地址
cache.cluster.properties为集群属性

3.oacache的应用

jsp页面缓存,cache1.jsp

写道

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*" %>
<%@ taglib uri="/WEB-INF/oscache.tld" prefix="cache" %>

<html>
<body>

没有缓存的日期: <%= new Date() %><p>
<!--自动刷新-->
<cache:cache time="30">
30秒刷新缓存一次的日期: <%= new Date() %> 
</cache:cache>
<!--手动刷新-->
<cache:cache key="testcache">
手动刷新缓存的日期: <%= new Date() %> <p>
</cache:cache>
<a href="cache2.jsp">手动刷新</a>

</body>
</html>

cache2.jsp

Java代码 

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

2. <%@ taglib uri="/WEB-INF/oscache.tld" prefix="cache" %> 

3. <html> 

4. <body> 

5. 

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

7. 

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

9. 

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

11. 

12. </body> 

13. </html> 

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

<%@ taglib uri="/WEB-INF/oscache.tld" prefix="cache" %>

<html>

<body>

缓存已刷新...<p>

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

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

</body>

</html>

过滤器缓存

web.xml

Xml代码 

1. <filter> 

2. <filter-name>CacheFilter</filter-name> 

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

4. <init-param> 

5. <param-name>time</param-name> 

6. <param-value>3600</param-value> 

7. </init-param> 

8. <init-param> 

9. <param-name>scope</param-name> 

10. <param-value>session</param-value> 

11. </init-param> 

12. </filter> 

13. <filter-mapping> 

14. <filter-name>CacheFilter</filter-name> 

15. <url-pattern>/selectedUser4UserPopupSelect.do</url-pattern> 

16. </filter-mapping> 

17. <filter-mapping> 

18. <filter-name>CacheFilter</filter-name> 

19. <url-pattern>/userPopupSelector.do</url-pattern> 

20. </filter-mapping> 

21. <filter-mapping> 

22. <filter-name>CacheFilter</filter-name> 

23. <url-pattern>/selectedUserFrame4UserPopupSelect.do</url-pattern> 

24. </filter-mapping> 

25. <filter-mapping> 

26. <filter-name>CacheFilter</filter-name> 

27. <url-pattern>/listUserFrame4UserPopupSelect.do</url-pattern> 

28. </filter-mapping> 

29. <filter-mapping> 

30. <filter-name>CacheFilter</filter-name> 

31. <url-pattern>/listUser4PopupSelect.do</url-pattern> 

32. </filter-mapping> 

33. <filter-mapping> 

34. <filter-name>CacheFilter</filter-name> 

35. <url-pattern>/popupAllOrganizationTree.do</url-pattern> 

36. </filter-mapping> 

37. <filter-mapping> 

38. <filter-name>CacheFilter</filter-name> 

39. <url-pattern>/index.do</url-pattern> 

40. </filter-mapping> 

41. <filter> 

42. <filter-name>osCacheFilter</filter-name> 

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

44. <init-param> 

45. <param-name>time</param-name> 

46. <param-value>3600</param-value> 

47. </init-param> 

48. <init-param> 

49. <param-name>scope</param-name> 

50. <param-value>application</param-value> 

51. </init-param> 

52. </filter> 

53. <filter-mapping> 

54. <filter-name>osCacheFilter</filter-name> 

55. <url-pattern>/listBig4Entity.do</url-pattern> 

56. </filter-mapping> 

57. <filter-mapping> 

58. <filter-name>osCacheFilter</filter-name> 

59. <url-pattern>/listSubEntity4Index.do</url-pattern> 

60. </filter-mapping> 

 <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>3600</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>/selectedUser4UserPopupSelect.do</url-pattern> 

</filter-mapping>

<filter-mapping> 

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

<url-pattern>/userPopupSelector.do</url-pattern> 

</filter-mapping> 

<filter-mapping> 

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

<url-pattern>/selectedUserFrame4UserPopupSelect.do</url-pattern> 

</filter-mapping> 

<filter-mapping> 

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

<url-pattern>/listUserFrame4UserPopupSelect.do</url-pattern> 

</filter-mapping> 

<filter-mapping> 

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

<url-pattern>/listUser4PopupSelect.do</url-pattern> 

</filter-mapping> 

<filter-mapping> 

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

<url-pattern>/popupAllOrganizationTree.do</url-pattern> 

</filter-mapping> 

<filter-mapping> 

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

<url-pattern>/index.do</url-pattern> 

</filter-mapping> 

<filter> 

<filter-name>osCacheFilter</filter-name> 

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

<init-param> 

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

<param-value>3600</param-value> 

</init-param> 

<init-param> 

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

<param-value>application</param-value> 

</init-param> 

</filter>  

<filter-mapping> 

<filter-name>osCacheFilter</filter-name> 

<url-pattern>/listBig4Entity.do</url-pattern> 

</filter-mapping>

<filter-mapping> 

<filter-name>osCacheFilter</filter-name> 

<url-pattern>/listSubEntity4Index.do</url-pattern> 

</filter-mapping> 

4 开始使用OSCache中的缓存组件 


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>

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
深究OSCache
应用OSCache提升J2EE系统运行性能
Appfuse1.9.3 for springMVC源码解读(1)-- 从web.xml
HTTP/1.1 Cache-Control的理解 - ゞ沉默是金ゞ - BlogJava
提高EXTJS执行速度:试试tk-filters
javaweb——Filter(过滤器)常见应用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服