用Jcs缓存机制创建更快的Web应用

原帖:http://blog.csdn.net/fireofstar/article/details/3639191
要使用jcs,需要这几个jar 包:concurrent.jar,jcs-1.3.jar,c ommons-logging.jar, commons-lang.jar,commons-collection.jar.

然后在web-info/classes下新建jcs的配置文件:cache.ccf,文件名不能改.内容如下:

# CACHE REGIONS AVAILABLE  
# Regions preconfigured for caching  
jcs.region.tempCache=  
jcs.region.tempCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes  
jcs.region.tempCache.cacheattributes.MaxObjects=1200  
jcs.region.tempCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache  
jcs.region.tempCache.elementattributes.IsEternal=false  
jcs.region.tempCache.elementattributes.MaxLifeSeconds=7200  
jcs.region.tempCache.elementattributes.IdleTime=1800  
jcs.region.tempCache.elementattributes.IsSpool=true  
jcs.region.tempCache.elementattributes.IsRemote=true  
jcs.region.tempCache.elementattributes.IsLateral=true  
# AUXILIARY CACHES AVAILABLE  
# Primary Disk Cache -- faster than the rest because of memory key storage  
jcs.auxiliary.DC=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory  
jcs.auxiliary.DC.attributes=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes  
jcs.auxiliary.DC.attributes.DiskPath=/usr/opt/bookstore/raf  #路径,在window下可改为c:/raf
jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000  
jcs.auxiliary.DC.attributes.MaxKeySize=10000  
jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000  
jcs.auxiliary.DC.attributes.MaxRecycleBinSize=7500  

 通过JSC jsc=JCS.getInstance("tempCache")取得缓存对象.

 取得JCS实例以后,就可以调用大多数需要的方法。put方法把一个新的对象放到缓存区中。键(第一个参数)和值(第二个参数)是调用这个方法所需要的全部内容。下面是一个基本的例子:

string key;
string value;
jcs.put(key,value)

JCS提供的 get方法取得缓存中的对象:

String cachedData = (String)jcs.get(key);
通过if (cachedData != null) {}来判断是缓存中是否存在对象。

最后需要的更多的共通的缓存功能是清楚JCS的缓存项目和工作完成后的缓存区。JCS提供了一个clear方法,调用这个方法可以从缓存区删除所有的缓存数据,同时还提供了一个remove方法,用于删除指定的缓存项目。dispose方法也是有效的,它用于释放被初始化的JCS缓存区。下面列出了这些方法的使用方法: 

// Dispose of a specific cached item
cache.remove(key);
// Dispose of all cache data
cache.clear();
// Dispose of the cache region
cache.dispose();

缓存元数据

JCS提供了比我们前面介绍的基本内容要丰富的多的功能。例如,它提供了搜集缓存对象和缓存区的元数据的功能,可以很容易的取得一下内容:

 ・缓存的键名;

 ・缓存项目被创建的时间;

 ・缓存的最大生存周期;

 ・被缓存项目的生存周期

下面的代码显示了怎样获取缓存项目的元数据。
try {
  JCSAdminBean admin = new JCSAdminBean();
  LinkedList linkedList = admin.buildElementInfo(regionName);
  ListIterator iterator = linkedList.listIterator();


  while (iterator.hasNext()) {
    CacheElementInfo info = (CacheElementInfo)iterator.next();
    System.out.println("Key: " + info.getKey());
    System.out.println("Creation Time: " + info.getCreateTime());
    System.out.println("Maximum Life (seconds): " + info.getMaxLifeSeconds());
    System.out.println("Expires in (seconds): " + info.getExpiresInSeconds());
  }
} catch (Exception e) {
}

对于被缓存项目的元数据是有用的,但是对于每个缓存区的元数据也是有益的。这些信息能够让你知道有多少缓存数据进入了缓存区,包括缓存失败还是成功,以及缓存的更新,下面的代码显示怎样搜集这些信息。
try {
  JCSAdminBean admin = new JCSAdminBean();
  LinkedList linkedList = admin.buildCacheInfo();
  ListIterator iterator = linkedList.listIterator();

  while (iterator.hasNext()) {
    CacheRegionInfo info = (CacheRegionInfo)iterator.next();
    CompositeCache compCache = info.getCache();
    System.out.println("Cache Name: " + compCache.getCacheName());
    System.out.println("Cache Type: " + compCache.getCacheType());
    System.out.println("Cache Misses (not found): " + compCache.getMissCountNotFound());
    System.out.println("Cache Misses (expired): " + compCache.getMissCountExpired());
    System.out.println("Cache Hits (memory): " + compCache.getHitCountRam());
    System.out.println("Cache Updates: " + compCache.getUpdateCount());
  }
} catch (Exception e) {
}


搜集缓存区和缓存对象的元数据,来帮助分析Web站点的那个缓存区或缓存对象需要被优化。元数据还会帮助你管理时间敏感性的缓存数据,对于特定的需要更新数据的用户,可以使用最大的生命周期和每个缓存项目的终止时间来刷新缓存数据。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值