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)
在J2EE系统中,我们经常需要处理一些特殊的动态内容,这些内容在一个时间段内的变更非常有限,但是又不得不将他们确定为动态内容进行输出,而且非常消耗数据库系统资源或者web服务器的资源,这时我们就可以采用Cache----一种用于提高系统响应速度、改善系统运行性能的技术----来优化我们的系统。尤其是在Web应用中,这种处理可以很显著的改善系统运行性能。
本文中作者给大家介绍一个实现J2EE框架中Web应用层缓存功能的开放源代码项目----OSCache。它提供了在J2EE系统中实现缓存需要的丰富的功能。通过应用OSCache,我们不但可以实现通常的Cache功能、自由的设定cache的相关特性比如缓存时间段/缓存内容等,提升系统性能,而且还能有效的改善系统的稳定性。除此之外,OSCache组件还提供了更多的特性比如集群、容错、灵活的缓存区选择等。
作者根据自己的使用经验给大家提供了一些简单的例子,他们部分演示了如何使用OSCache组件提供的丰富特性,OSCache提供的特性远不止这些,需要大家在今后的时间里深入的研究,同时也希望大家通过E-mail和作者贡献研究成果。 --源码分析
oscache的代码量很少。分析源码的时候,主要分析com.opensymphony.oscache.base.Cache类就可以,这个又是整个oscache的核核心类,除此之外,都是辅助类。oscache用很少的代码量来实现这么强大的功能,应该说代码还是很值得阅读的。
com.opensymphony.oscache.base.Cache的api中主要的方法如下:
public void putInCache(String key, Object content, String[] groups, EntryRefreshPolicy policy, String origin);
public void getFromCache(String, int, String);
public void cancelUpdate(String key);
如何使用,引用官方的docs中的一段例子如下:
String myKey = "myKey";
String myValue;
int myRefreshPeriod = 1000;
try {
// Get from the cache
myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
} catch (NeedsRefreshException nre) {
try {
// Get the value (probably from the database)
myValue = "This is the content retrieved.";
// Store in the cache
admin.putInCache(myKey, myValue);
} catch (Exception ex) {
// We have the current content if we want fail-over.
myValue = (String) nre.getCacheContent();
// It is essential that cancelUpdate is called if the
// cached content is not rebuilt
admin.cancelUpdate(myKey);
}
}
这段代码是非常使用oscache的经典代码。细说如下myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);从对象缓存map中取出myKey对应的对象,两种情况可能发生: 一:如果myKey对应的对象存在(先前putInCache)并且没有过期(isStale()方法)那么getFromCache方法会正常返回。二:如果对应的对象不存在或者过期,又分为两种情况: 1)请求的线程第一个探测到对象过期,那么这个时候oscache会抛给client一个NeedRefreshException, 提示client,需要对数据进行一下刷新,怎么刷新,putInCache即可。 2)如果请求的线程并非第一个探测到对象过期,两种情况:I如果oscache.properties中对blocking设置为true,那么该线程会在此阻塞,直到putInCache在另一个线程中被调用或者是cancelUpdate被调用。II否则情况跟1)类型,也会出现NeedRefreshException.
上面的那个例子之所以要在catch(NeedRefreshException)中进行putInCache,又之所以要在putInCache的catch(Exception)中做cancelUpdate道理就在此,我们不希望看到一个线程在getFromCache的位置一直阻塞下去。
补充: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!!