JCS入门指南

来源:互联网 发布:java的设计模式 编辑:程序博客网 时间:2024/06/05 19:20

文章出处:http://guoshiguan.iteye.com/blog/811677


概述

使用JCS需要完成以下骤:
1.    了解核心概念。
2.    下载JCS
3.    获得JCS依赖
4.    配置JCS
5.    开始编程应用
入门指南的目的是尽可能快帮助你搭建和运行JCS,JCS各种各样的特性的深入文档在用户指南中提供。

第一步 了解核心的概念 
为了使用JCS,你必须知道一些核心的概念,最重要是你需要知道”elements”,”regions”,和“auxiliaries”这间有什么不同
JCS是一个对象缓存,你能放置一些对象或是”elements”并通过key来访问它们,很象一个hashtable。
你可以想象JCS是一个能过Name来获取的hashtables的集合。其中每一个hashtables都被称做“region”,每一个region都能被独立于其他regions配置。例如,我可以有一个称做城市的region,我缓存了一些定期被改变的城市对象。我也可以定义一个region被叫做产品,我缓存一些定期改变的产品数据。我将可以配置易变的产品的region elements 过期时间要快于city的region
Auxiliaries是region能用的插件选项。核心的Auxiliaries是Indexed Disk Cache(磁盘索引缓存)、TCP Lateral Cache(TCP横向缓存)、Remote Cache Server(远程缓存服务器)。例如,磁盘缓存允许当你的内存达到阈值后把缓存对象交换到硬上。
第二步下载jcs 

第三步,获得JCS依赖 

第四步,配置JCS 

JCS配置在一个叫"cache.ccf"的配置文件中,有替代品使用此文件,但他们超越了入门指南的范围。
缓存配置分为三部分:默认配置,regions配置和auxiliaries配置。你可以把auxiliaries想象为log4j的log4j  appenders,regions是log4j categories。每一个region都能指定auxiliaries。如果一个region不在Cache.ccf进行配置,则它将会使用默认的配置。以JCS和log4j 不同的是JCS中预先定义的region不能使default region内auxiliaries。
下面的cache.ccf文件定义了叫做”testCache1”的region,这个region使用了Indexed Disk Cache的axiliaries,它在这里被默认叫做DC。LRU内存缓存被选择用来管理内存


# DEFAULT CACHE REGIONjcs.default=DCjcs.default.cacheattributes=    org.apache.jcs.engine.CompositeCacheAttributesjcs.default.cacheattributes.MaxObjects=1000jcs.default.cacheattributes.MemoryCacheName=    org.apache.jcs.engine.memory.lru.LRUMemoryCachejcs.default.cacheattributes.UseMemoryShrinker=falsejcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=3600jcs.default.cacheattributes.ShrinkerIntervalSeconds=60jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributesjcs.default.elementattributes.IsEternal=falsejcs.default.elementattributes.MaxLifeSeconds=21600jcs.default.elementattributes.IdleTime=1800jcs.default.elementattributes.IsSpool=truejcs.default.elementattributes.IsRemote=truejcs.default.elementattributes.IsLateral=true# PRE-DEFINED CACHE REGIONSjcs.region.testCache1=DCjcs.region.testCache1.cacheattributes=    org.apache.jcs.engine.CompositeCacheAttributesjcs.region.testCache1.cacheattributes.MaxObjects=1000jcs.region.testCache1.cacheattributes.MemoryCacheName=    org.apache.jcs.engine.memory.lru.LRUMemoryCachejcs.region.testCache1.cacheattributes.UseMemoryShrinker=falsejcs.region.testCache1.cacheattributes.MaxMemoryIdleTimeSeconds=3600jcs.region.testCache1.cacheattributes.ShrinkerIntervalSeconds=60jcs.region.testCache1.cacheattributes.MaxSpoolPerRun=500jcs.region.testCache1.elementattributes=org.apache.jcs.engine.ElementAttributesjcs.region.testCache1.elementattributes.IsEternal=false# AVAILABLE AUXILIARY CACHESjcs.auxiliary.DC=    org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactoryjcs.auxiliary.DC.attributes=    org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributesjcs.auxiliary.DC.attributes.DiskPath=${user.dir}/jcs_swapjcs.auxiliary.DC.attributes.MaxPurgatorySize=10000000jcs.auxiliary.DC.attributes.MaxKeySize=1000000jcs.auxiliary.DC.attributes.MaxRecycleBinSize=5000jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000jcs.auxiliary.DC.attributes.ShutdownSpoolTimeLimit=60

第五步:JCS编码 
JCS提供了一个方便的类,org.apache.jcs.JCS这个类将可以满足你的需要。它可以仅仅通用region Name来获取一个region。如果你想要使用JCS的City 集合,你可以如以下使用:

import org.apache.jcs.JCS;import org.apache.jcs.access.exception.CacheException;. . .    private static final String cacheRegionName = "city";    private JCS cache = null;. . .// in your constructor you might do this            try            {                setCache( JCS.getInstance( this.getCacheRegionName() ) );            }            catch ( CacheException e )            {                log.error( "Problem initializing cache for region name ["                  + this.getCacheRegionName() + "].", e );            }. . .            // to get a city out of the cache by id you might do this:            String key = "cityId:" + String.valueOf( id );            City city = (City) cache.get( key );. . .            // to put a city object in the cache, you could do this:            try            {                // if it isn't null, insert it                if ( city != null )                {                    cache.put( key, city );                }            }            catch ( CacheException e )            {                 log.error( "Problem putting "                   + city + " in the cache, for key " + key, e );            }