Oscache使用详解,配置,使用,细节

  1. 1、OSCache是什么?   

  2.   OSCache标记库由OpenSymphony设计,它是一种开创性的缓存方案,它提供了在现有JSP页面之内实现内存缓存的功能。OSCache是个一个被广泛采用的高性能的J2EE缓存框架,OSCache还能应用于任何Java应用程序的普通的缓存解决方案。

  3. 2、OSCache的特点 

  4.  (1) 缓存任何对象:你可以不受限制的缓存部分jsp页面或HTTP请求,任何java对象都可以缓存。  

  5.  (2) 拥有全面的API:OSCache API允许你通过编程的方式来控制所有的OSCache特性。

  6.  (3) 永久缓存:缓存能被配置写入硬盘,因此允许在应用服务器的多次生命周期间缓存创建开销昂贵的数据。 

  7.  (4) 支持集群:集群缓存数据能被单个的进行参数配置,不需要修改代码。 

  8.  (5) 缓存过期:你可以有最大限度的控制缓存对象的过期,包括可插入式的刷新策略(如果默认性能不能满足需要时)。

  9. 3、OSCache缓存的三种模式

  10.  (1)缓存于内存中

  11.  (2)缓存于硬盘中

  12.  (3)一种较灵活的方式,先缓存于内存中,如果满了则转至硬盘

  13. 4、OSCache的安装与配置   

  14. OSCache是当前运用最广的缓存方案,JBoss,Hibernate,Spring等都对其有支持

  15.  

  16. 下面简单介绍一下OSCache的配置和使用过程。

  17. 1.安装过程

  18. 从http://www.opensymphony.com/oscache/download.html下载合适的OSCache版本

  19. 解压压缩文件得到文件夹oscache-2.4.1-full

  20.  

  21. 从解压缩目录取得oscache.jar 文件放到/WEB-INF/lib 或相应类库目录中,jar文件名可能含有版本号和该版本的发布日期信息等,如oscache-2.4.1.jar

  22. 将lib下的commons-logging.jar和jgroups-all.jar都放入到/WEB-INF/lib中

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

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

  25.  

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

  27.  

  28. 内容如下:

  29. Cache.manory=false;

  30. cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.HashPersistenceListener;

  31. win类路径类似为c:\\app\\cache

  32. unix类路径类似为/opt/myapp/cache

  33.  

  34.  

  35. 现在你的应用目录类似如下:

  36. $WEB_APPLICATION\WEB-INF\lib\oscache.jar

  37. $WEB_APPLICATION\WEB-INF\lib\commons-logging.jar

  38. $WEB_APPLICATION\WEB-INF\lib\jgroups-all.jar

  39. $WEB_APPLICATION\WEB-INF\classes\oscache.properties

  40. $WEB_APPLICATION\WEB-INF\classes\oscache.tld

  41.  

  42. 将下列代码加入web.xml文件中即可使用OSCache提供的标签了

  43.  <jsp-config>

  44.   <taglib>

  45.     <taglib-uri>oscache</taglib-uri>

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

  47.   </taglib>

  48.  </jsp-config>

  49. 2.oscache.properties 文件配置向导(源文件已经写好了只要将语句前面的#号去掉即可)

  50.  

  51. cache.memory值为true 或 false ,默认为在内存中作缓存,如设置为false,即将缓存的数据存到cache.path配置好的路径下

  52.  

  53. cache.capacity:缓存元素个数

  54.  

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

  56.  

  57. cache.cluster 相关

  58. 为集群设置信息。

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

  60. cache.cluster.properties为集群属性

  61.  

  62. 5.OSCache的基本用法

  63. 1.OSCache标签的使用

  64. 注意:要使用标签必须在web.xml中配置好之后再在页面中引入,在页面开始加入<%@ taglib uri="oscache"prefix="cache" %>句即可

  65. 以下两个页面是使用OSCache标签的例子:

  66.  

  67. index1.jsp 内容如下

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

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

  70. <html>

  71. <body>

  72. <!—未使用到标签-->

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

  74.  

  75. <!--自动刷新但是没有给标记符key赋值-->

  76. <cache:cache time="30">

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

  78. </cache:cache>

  79.  

  80. <!--手动刷新,给标记符key赋值了-->

  81. <cache:cache key="testcache">

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

  83. </cache:cache>

  84. <ahref="index2.jsp">手动刷新</a>

  85. </body>

  86. </html>

  87. index2.jsp 执行手动刷新页面如下:

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

  89. <html>

  90. <body>

  91.  

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

  93. <!—根据key定位到index1.jsp页面的testcache,范围为application,使用的是flush标签用来清空缓存-->

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

  95. <a href="index1.jsp">返回</a>

  96. </body>

  97. </html>

  98.  

  99. 你也可以通过下面语句定义Cache的有效范围,如不定义scope,scope默认为Applcation

  100. <cache:cache time="30"scope="session">

  101.   内容...

  102. </cache:cache>

  103.  

  104. 6. 缓存过滤器 CacheFilter

  105. 如果是对页面进行缓存则需要在web.xml中定义缓存过滤器,定义特定资源的缓存。

  106. <filter>

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

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

  109.   <init-param>

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

  111.     <param-value>60</param-value>

  112.   </init-param>

  113.   <init-param>

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

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

  116.   </init-param>

  117. </filter>

  118.  

  119. <filter-mapping>

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

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

  122. </filter-mapping>

  123.  

  124. 上面定义将缓存所有.jsp页面,缓存刷新时间为60秒,缓存作用域为Session

  125.  

  126. 注意:如果要加入其它的属性的话只要添加一个<init-param>标签对,在其中添加相应的属性,用法跟在jsp页面使用标签是一致的

  127.  

  128. 注意,CacheFilter只捕获Http头为200的页面请求,即只对无错误请求作缓存,而不对其他请求(如500,404,400)作缓存处理

  129.  

  130. 7、主要的标签以及标签属性

  131. Cache标签——缓存代码段主要使用到的标签:

  132. 属性说明:

  133. key
     标识缓存内容的关键词。在指定的作用范围内必须是唯一的。默认的key是被访问页面的URI和后面的请求字符串。可以在同一个页面中使用很多cache 标签而不指定他的key属性,这种情况下系统使用该页面的URI和后面的请求字符串,另外再自动给这些key增加一个索引值来区分这些缓存内容。但是不推 荐采用这样的方式。

    scope
     缓存发生作用的范围,可以是application或者session。默认为application。

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

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

  134.  

  135. 注意:time与duration的区别是time的单位是秒所以设置的时候是只要输入一个阿拉伯数字,而duration的话可以使用简单日期格式或者符合USO-8601的日期格式。如:duration='PT5M' duration='5s'等


  136. cron
        指定缓存内容失效表达式。
        通过Cron表达式我们可以很灵活的设置缓存的失效时间,Cron表达式包括5个字段分别为Minute,Hour, DOM(Day Of Month), Month,DOW(Day Of Week)。他们顺序地对应了5个位置。当某个位置上的值为*时,表示该位置上的任意时间。另外还提供了指定时间的操作符号"-",",","/",他们 分别表示一段时间范围,具体的时间,以及递增的时间段。下面是几个例子说明一下Cron表达式的基本应用:
            (1) "10/20 * * * *" :因是第一个位置,并且是一个递增的表达式,所以表达式指定的是每个小时的第10分钟,第30分钟,第50分钟缓存内容失效。
            (2) "* 8-18/4 * * *" :指定每天早上8点到晚上6点之间,每4个小时缓存内容失效。  等同于"*8,12,16 * * *"。
            (3) "* * * * 1-5":表示每个星期一到星期五内容失效。

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

    mode
     如果不希望被缓存的内容增加到给用户的响应中,可以设置mode属性为"silent"。此时被缓存的部分不在页面上显示,而其它任意的mode属性值都会将缓存的部分显示到页面上。

    groups
     指定当前cache标签所属的组,可使用“,”分割组名。这样就可以对缓存项进行分组了。如果缓存项依赖于应用的其它部分或其它数据,分组就有了用武之地——当这种依赖改变时(刷新相关的组),这个组的所有缓存项都将过期。

    language
     使用ISO-639定义的语言码来发布不同的缓存内容(under an otherwise identical key)。要在一个多语言网站上同一段JSP代码不同用户的参数提供不同的语言时,这个属性会很有用。

    refreshpolicyclass

  137. 指定自定义的刷新策略类的全限定类名。这个类继承自

  138. com.opensymphony.oscache.web.WebEntryRefreshPolicy

    refreshpolicyparam
     指定任意需要传给refreshpolicyclass的参数。如果没有指定refreshpolicyclass,则这个值不起作用。

  139.  

  140. usecached标签
            <usecached />:必须嵌套在<cache>标签中。
    属性说明:

    use
     告诉所在的<cache>标签是否使用已经缓存的内容(缺省为true,使用缓存的内容)。可以使用这个标签来控制缓存。比如使用<frush>标签刷新某个key的缓存,但可以在必要的地方即使这样的强制刷新也仍然使用缓存内容而不刷新。

  141.  


  142. flush标签
            这个标签用于在运行时刷新缓存。只有运行flush标签后再次访问相关缓存项时才执行刷新。
    属性说明:

    scope[all]
     指定要刷新的范围。可选的值是"application","session" 和 nul。null(到底是null量还是all呀)值指定刷新所有的缓存(是指使用cache标签的缓存)。

    key
     当指定了scope和key值时,刷新唯一的缓存项。当这个缓存项下次被访问时将被刷新。只指定一个key值而没有指定scope不起作用。

    group
     指定一个组时将刷新所有这个组中的缓存项。只指定一个group值而没有指定scope不起作用。

    pattern
     任意包含pattern属性指定的值的缓存项都将被刷新。只指定一个pattern值而没有指定scope不起作用。 (注意:OSCache项目组已经不赞成使用pattern这个属性赖刷新缓存,二是鼓励使用具有更好的灵活性和性能的group属性来代替)

    language
     使用ISO-639定义的语言码来发布不同的缓存内容(under an otherwise identical key)。要在一个多语言网站上同一段JSP代码不同用户的参数提供不同的语言时,这个属性会很有用。

  143. addgroup标签
            <addgroup />:必须嵌套在<cache>标签中。
    属性说明: 

    group
    定义cache标签的group名称

  144. 示例代码如下:
    <oscache:cache key="test1">
        <oscache:addgroupgroup="group1" />
            ... some jsp content ...
        <oscache:addgroupgroup="group2" />
        ... some more jsp content ...
    </oscache:cache>

  145. #一、内存缓存或硬盘、数据库缓存  

  146. cache.memory=false  

  147.   

  148. #二、缓存的最大数量。默认是不限制,cache不会移走任何缓存内容。负数被视不限制。  

  149. cache.capacity=100  

  150.   

  151. #三、运算规则。为了使用规则,cache的size必须是指定的。  

  152. #如果cache的size不指定的话, 将不会限制缓存对象的大小。如果指定了cache的size,但不指定algorithm,那它会默认使用:com.opensymphony.oscache.base.algorithm.LRUCache  

  153. #有下面三种规则:  

  154. #*com.opensymphony.oscache.base.algorithm.LRUCache:   

  155. #last in first out(最后插入的最先调用)。默认选项。  

  156. #*com.opensymphony.oscache.base.algorithm.FIFOCache:  

  157. #first int first out(最先插入的最先调用)。  

  158. #*com.opensymphony.oscache.base.algorithm.UnlimitedCache :   

  159. #cache中的内容将永远不会被丢弃。  

  160. #如果cache.capacity不指定值的话,它将被设为默认选项。  

  161. cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache  

  162.   

  163. #四、是否同步。true 或者 false。一般设为true,避免读取脏数据。  

  164. cache.blocking=true  

  165.   

  166. #五、指定硬盘缓存是否要作限制。默认值为falsefalse的状况下,disk cache capacity 和cache.capacity的值相同。  

  167. cache.unlimited.disk=false  

  168.   

  169. #六、指定类是被持久化缓存的类。class必须实现PersistenceListener接口。  

  170. #作为硬盘持久,可以实现com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener接口。  

  171. #它把class的toString()输出的hash值作为文件的名称。如果你要想文件名易读些(自己设定),DiskPersistenceListener 的父类也能使用,但其可能有非法字符或者过长的名字。  

  172. #注意:HashDiskPersistenceListener 和 DiskPersistenceListener 需要设定硬盘路径:cache.path  

  173. #cache.persistence.class=  

  174.   

  175. #七、指定硬盘缓存的路径。目录如果不存在将被建立。同时注意oscache应该要有权限写文件系统。  

  176. #例: cache.path=c:\\myapp\\cache  cache.path=/opt/myapp/cache  

  177. cache.path=e:\\index\\cache  

  178.   

  179. #八、指定是否只有在内存不足的情况下才使用硬盘缓存。  

  180. #默认值false。但推荐是true如果内存cache被允许的话。这个属性彻底的改变了cache的行为,使得persisted cache和memory是完全不同。  

  181. cache.persistence.overflow.only=false  

  182.   

  183. #九、class名列表(用逗号隔开)。每个class必须实现以下接口中的一个 或者几个  

  184. #CacheEntryEventListener:接收cache add/update/flush and remove事件  

  185. #CacheMapAccessEventListener :接收cache访问事件。这个可以让你跟踪cache怎么工作。  

  186. #默认是不配置任何class的。当然你可以使用一下的class:  

  187. #*com.opensymphony.oscache.plugins.clustersupport.BroadcastingCacheEventListener : 分布式的监听器。可以广播到局域网内的其他cache实例。  

  188. #* com.opensymphony.oscache.extra.CacheEntryEventListenerImpl :一个简单的监听器。在cache的生命周期中记录所有entry的事件。  

  189. #* com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl : 记录count of cache map events(cache hits,misses and state hits).  

  190. #cache.event.listeners  

  191.   

  192. #十、在application 和 session的作用域时 用于标识cache 对象的,  用于ServletCacheAdministrator;此属性不是指定为"__oscache_cache"格式时为默认值, 如果代码中需要用到默认值时可以通使用com.opensymphony.oscache.base.Const.DEFAULT_CACHE_KEY 来取得;  

  193. cache.key=__oscache_cache  

  194.   

  195. #十一、当配置多个服务器时,想通过服备器名称自动生成cache key时,可将此属性设为true. 默认值为false;  

  196. cache.use.host.domain.in.key=true  

  197.   

  198. #十二、    在以上基础选项之上可以加入一些额外的属性到此文件中.例:  JavaGroupsBroadcastingListener 便是额外的  

  199. #Additional Properties  

  200.   

  201. #十三、用于缓存集群. 默认为231.12.21.132  

  202. #cache.cluster.multicast.ip  

  203.   

  204. #十四、    指集群中的额外配置项. 以下是默认设置:(此属性的相关说将在集群文档中说明)  

  205. #UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;\  

  206. #mcast_send_buf_size=150000;mcast_recv_buf_size=80000):\  

  207. #PING(timeout=2000;num_initial_members=3):\  

  208. #MERGE2(min_interval=5000;max_interval=10000):\  

  209. #FD_SOCK:VERIFY_SUSPECT(timeout=1500):\  

  210. #pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):\  

  211. #UNICAST(timeout=300,600,1200,2400):\  

  212. #pbcast.STABLE(desired_avg_gossip=20000):\  

  213. #FRAG(frag_size=8096;down_thread=false;up_thread=false):\  

  214. #pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)  

  215. #cache.cluster.properties  

  216.   

  217.   

  218. # CACHE IN MEMORY  

  219. #  

  220. # If you want to disable memory caching, just uncomment this line.  

  221. #  

  222. # cache.memory=false  

  223.   

  224.   

  225. # CACHE KEY  

  226. #  

  227. # This is the key that will be used to store the cache in the application  

  228. # and session scope.  

  229. #  

  230. # If you want to set the cache key to anything other than the default  

  231. # uncomment this line and change the cache.key  

  232. #  

  233. # cache.key=__oscache_cache  

  234.   

  235.   

  236. # USE HOST DOMAIN NAME IN KEY  

  237. #  

  238. # Servers for multiple host domains may wish to add host name info to  

  239. # the generation of the key.  If this is true, then uncomment the  

  240. # following line.  

  241. #  

  242. # cache.use.host.domain.in.key=true  

  243.   

  244.   

  245. # CACHE LISTENERS  

  246. #  

  247. # These hook OSCache events and perform various actions such as logging  

  248. # cache hits and misses, or broadcasting to other cache instances across a cluster.  

  249. # See the documentation for further information.  

  250. #  

  251. # cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JMSBroadcastingListener,  \  

  252. #                       com.opensymphony.oscache.extra.CacheEntryEventListenerImpl,               \  

  253. #                       com.opensymphony.oscache.extra.CacheMapAccessEventListenerImpl,           \  

  254. #                       com.opensymphony.oscache.extra.ScopeEventListenerImpl,                    \  

  255. #                       com.opensymphony.oscache.extra.StatisticListenerImpl  

  256.   

  257.   

  258. # CACHE PERSISTENCE CLASS  

  259. #  

  260. # Specify the class to use for persistence. If you use the supplied DiskPersistenceListener,  

  261. # don't forget to supply the cache.path property to specify the location of the cache  

  262. # directory.  

  263. #   

  264. # If a persistence class is not specified, OSCache will use memory caching only.  

  265. #  

  266. # cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener  

  267. # cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.HashDiskPersistenceListener  

  268.   

  269. # CACHE OVERFLOW PERSISTENCE  

  270. # Use persistent cache in overflow or not. The default value is false, which means  

  271. # the persistent cache will be used at all times for every entry.  true is the recommended setting.  

  272. #  

  273. # cache.persistence.overflow.only=true  

  274.   

  275. # CACHE DIRECTORY  

  276. #  

  277. # This is the directory on disk where caches will be stored by the DiskPersistenceListener.  

  278. # it will be created if it doesn't already exist. Remember that OSCache must have  

  279. # write permission to this directory.  

  280. #  

  281. # Note: for Windows machines, this needs \ to be escaped  

  282. # ie Windows:  

  283. # cache.path=c:\\myapp\\cache  

  284. # or *ix:  

  285. # cache.path=/opt/myapp/cache  

  286. #  

  287. # cache.path=c:\\app\\cache  

  288.   

  289.   

  290. # CACHE ALGORITHM  

  291. #  

  292. # Default cache algorithm to use. Note that in order to use an algorithm  

  293. # the cache size must also be specified. If the cache size is not specified,  

  294. # the cache algorithm will be Unlimited cache.  

  295. #  

  296. # cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache  

  297. # cache.algorithm=com.opensymphony.oscache.base.algorithm.FIFOCache  

  298. # cache.algorithm=com.opensymphony.oscache.base.algorithm.UnlimitedCache  

  299.   

  300. # THREAD BLOCKING BEHAVIOR  

  301. #  

  302. # When a request is made for a stale cache entry, it is possible that another thread is already  

  303. # in the process of rebuilding that entry. This setting specifies how OSCache handles the  

  304. # subsequent 'non-building' threads. The default behaviour (cache.blocking=false) is to serve  

  305. # the old content to subsequent threads until the cache entry has been updated. This provides  

  306. # the best performance (at the cost of serving slightly stale data). When blocking is enabled,  

  307. # threads will instead block until the new cache entry is ready to be served. Once the new entry  

  308. # is put in the cache the blocked threads will be restarted and given the new entry.  

  309. # Note that even if blocking is disabled, when there is no stale data available to be served  

  310. # threads will block until the data is added to the cache by the thread that is responsible  

  311. for building the data.  

  312. #  

  313. # cache.blocking=false  

  314.   

  315. # CACHE SIZE  

  316. #  

  317. # Default cache size in number of items. If a size is specified but not  

  318. # an algorithm, the cache algorithm used will be LRUCache.  

  319. #  

  320. #cache.capacity=1000  

  321.   

  322.   

  323. # CACHE UNLIMITED DISK  

  324. # Use unlimited disk cache or not. The default value is false, which means  

  325. # the disk cache will be limited in size to the value specified by cache.capacity.  

  326. #  

  327. # cache.unlimited.disk=false  

  328.   

  329.   

  330. # JMS CLUSTER PROPERTIES  

  331. #  

  332. # Configuration properties for JMS clustering. See the clustering documentation  

  333. for more information on these settings.  

  334. #  

  335. #cache.cluster.jms.topic.factory=java:comp/env/jms/TopicConnectionFactory  

  336. #cache.cluster.jms.topic.name=java:comp/env/jms/OSCacheTopic  

  337. #cache.cluster.jms.node.name=node1  

  338.   

  339.   

  340. # JAVAGROUPS CLUSTER PROPERTIES  

  341. #  

  342. # Configuration properites for the JavaGroups clustering. Only one of these  

  343. # should be specified. Default values (as shown below) will be used if niether  

  344. # property is set. See the clustering documentation and the JavaGroups project  

  345. # (www.javagroups.com) for more information on these settings.  

  346. #  

  347. #cache.cluster.properties=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32;\  

  348. #mcast_send_buf_size=150000;mcast_recv_buf_size=80000):\  

  349. #PING(timeout=2000;num_initial_members=3):\  

  350. #MERGE2(min_interval=5000;max_interval=10000):\  

  351. #FD_SOCK:VERIFY_SUSPECT(timeout=1500):\  

  352. #pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800;max_xmit_size=8192):\  

  353. #UNICAST(timeout=300,600,1200,2400):\  

  354. #pbcast.STABLE(desired_avg_gossip=20000):\  

  355. #FRAG(frag_size=8096;down_thread=false;up_thread=false):\  

  356. #pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=true)  

  357. #cache.cluster.multicast.ip=231.12.21.132  

同一个应用可以实例化多个oscache的admin,使用不同的配置策略,如不同的配置文件。例如: 

Java代码  收藏代码

  1. Public class ChatRecordCache {  

  2.     private static GeneralCacheAdministrator admin;  

  3.     private static String OSCACHE_DISK =  "oscache_disk.properties";  

  4.     static {  

  5.         Properties prop = new Properties();   

  6.         try {  

  7.             prop.load(new FileInputStream(OSCACHE_DISK));  

  8.         } catch (FileNotFoundException e) {  

  9. //          e.printStackTrace();  

  10.         } catch (IOException e) {  

  11. //          e.printStackTrace();  

  12.         }  

  13.         admin = new GeneralCacheAdministrator(prop);  

  14.     }  

  15. }  

对应的配置文件为oscache_disk.properties: 

Java代码  收藏代码

  1. cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener  

  2. cache.path=/opt/myapp/cache  

  3. cache.algorithm=com.opensymphony.oscache.base.algorithm.FIFOCache  

  4. cache.capacity=1000  

  5. cache.unlimited.disk=true  



Java代码  收藏代码

  1. public class ChatUserCache {  

  2.     private static GeneralCacheAdministrator admin = new GeneralCacheAdministrator();  

  3. }  

此处采用默认的配置文件oscache.properties. 

注意:如果多个cache实例产生的硬盘缓存路径和缓存名称完全相同,将会无法区分,此时要注意设置不同的key值区分。 

关于NeedsRefreshException,引用官方文档中的一段例子如下: 

Java代码  收藏代码

  1. // ---------------------------------------------------------------  

  2. // Typical use with fail over  

  3. // 采取补救措施的典型方案  

  4. // ---------------------------------------------------------------  

  5.  String myKey = "myKey";  

  6.  String myValue;  

  7.  int myRefreshPeriod = 1000;  

  8.  try {  

  9.      // Get from the cache  

  10.      myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);  

  11.  } catch (NeedsRefreshException nre) {  

  12.     //This exception is thrown when retrieving an item from cache and it //is expired. Note that for fault tolerance purposes, it is possible //to retrieve the current cached object from the exception.  

  13.      try {  

  14.          // Get the value (probably by calling an EJB)  

  15.          myValue = "This is the content retrieved.";  

  16.          // Store in the cache  

  17.          admin.putInCache(myKey, myValue);//有解锁的作用  

  18.      } catch (Exception ex) {//获取myValue过程发生异常,无法执行到putInCache方法  

  19.          // We have the current content if we want fail-over.  

  20.          //采取补救措施,获取可能的值(适用于数据过期的情况)  

  21.     myValue = (String) nre.getCacheContent();  

  22.       

  23.          // It is essential that cancelUpdate is called if the  

  24.          // cached content is not rebuilt  

  25.          admin.cancelUpdate(myKey);//解锁  

  26.      }  

  27.  }  

这段代码是使用oscache的经典代码,细说如下。myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);从对象缓存map中取出myKey对应的对象,两种情况可能发生: 一:如果myKey对应的对象存在(先前putInCache)并且没有过期(isStale()方法)那么getFromCache方法会正常返回。二:如果对应的对象不存在或者过期,分为两种情况:1)请求的线程第一个探测到对象不存在或者过期,那么这个时候oscache会抛出一个NeedRefreshException, 提示需要对数据进行一下刷新,怎么刷新,putInCache或者cancelUpdate即可。2)如果请求的线程并非第一个探测到对象不存在,两种情况:A.之前探测到的线程没有进行刷新处理,直接阻塞。B. 之前探测到的线程进行了刷新处理,抛出NeedRefreshException。3)如果请求的线程并非第一个探测到对象过期,两种情况:A.之前探测到的线程没有进行刷新处理,直接阻塞。B. 之前探测到的线程进行了刷新处理,又分两种情况:I.如果oscache.properties中对blocking设置为false(默认cache.blocking=false), 抛出NeedRefreshException,此处采取补救措施nre.getCacheContent(),会取出现有(很可能过期)内容。 II. 如果cache.blocking=true那么该线程会在此阻塞,直到putInCache在另一个线程中被调用或者是cancelUpdate被调用。参考以下说明: 
cache.blocking 
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. 

补充:oscache对一个cacheEntry是否是Stale(或者说expire)的判断原则: 
1.Cache.flushEntry() 
2.Cache.flushAll() 
3.CacheEntry.setGroups(groups); Cache.flushGroup(group) 
4.createTime + refreshPeriod< now 
5.cronExpiry< now 
6.CacheEntry自带的EntryRefreshPolish的needRefresh方法返回true 
上面的6条中的任何一条如果为true, 那么CacheEntry就是stale的,need refresh! 
官方文档中的另一段例子如下: 

Java代码  收藏代码

  1. // ---------------------------------------------------------------  

  2. // Typical use without fail over  

  3. //不采取补救措施的典型方案  

  4. // ---------------------------------------------------------------  

  5.  String myKey = "myKey";  

  6.  String myValue;  

  7.  int myRefreshPeriod = 1000;  

  8.  try {  

  9.      // Get from the cache  

  10.      myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);  

  11.  } catch (NeedsRefreshException nre) {  

  12.      try {  

  13.          // Get the value (probably by calling an EJB)  

  14.          myValue = "This is the content retrieved.";  

  15.          // Store in the cache  

  16.          admin.putInCache(myKey, myValue);  

  17.          updated = true;  

  18.      } finally {  

  19.          if (!updated) {  

  20.              // It is essential that cancelUpdate is called if the  

  21.              // cached content could not be rebuilt  

  22.              admin.cancelUpdate(myKey);  

  23.          }  

  24.      }  

  25.  } 

猜你喜欢

转载自blog.csdn.net/wenminhao/article/details/80080984