OSCache的使用及配置

 

配置OSCache缓存步骤:
1.把oscache.properties文件放到src目录下边,然后把commons-logging-X.X.jar和oscache-2.4.jar包拷贝到lib目录下
2.使用标签用<%@ taglib uri="http://www.opensymphony.com/oscache" prefix="cache" %>
  <cache:cache key="" scope="" time="" refresh="true/false/${param.bool}">
  <%=new Date()  %>
  </cache:cache>

key:是对应的键值
scope:对应缓存的存放的区域application和session
time:默认的3600秒 一个小时,time是以秒为单位
refresh:true或false 强行清楚缓存

 

<cache:flush scope="application"/>
清除application范围内的所有缓存
<cache:flush scope="session" key="foobar"/>
清除session范围内的key为foobar的缓存
<cache:flush scope="application" group="currentcyData"/>
清除application范围内组名为currentcyData的所有缓存


<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>7200</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>/XX/*.jsp</url-pattern> 要过滤的路径 /xx/xx/*
    </filter-mapping>


oscache.properties  osCache的配置文件   放在 webapp/WEB-INF/classes/下;

oscache.properties 中的配置项如下:

1、cache.memory:

是否使用内存缓存; true 或 false。默认为true;

2.cache.capacity
缓存的最大数量。默认是不限制,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 的父类也

         能使用,但其可能有非法字符或者过长的名字。

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的生命周期中记录所有entry的事件。

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

10、cache.key

 在application 和 session的作用域时 用于标识cache 对象的,  用于ServletCacheAdministrator;

        此属性不是指定为"__oscache_cache"格式时为默认值, 如果代码中需要用到默认值时可以通使用com.opensymphony.oscache.base.Const.DEFAULT_CACHE_KEY

       来取得;

11、cache.use.host.domain.in.key

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

       

12、Additional Properties

  在以上基础选项之上可以加入一些额外的属性到此文件中.

       例:  JavaGroupsBroadcastingListener 便是额外的.

13、cache.cluster.multicast.ip
    


        用于缓存集群. 默认为231.12.21.132

14、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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值