文章出處: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 ); }