概述

使用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被叫做产品,我缓存一些定期改变的产品数据。我将可以配置易变的产品的regionelements过期时间要快于city的region

Auxiliaries是region能用的插件选项。核心的Auxiliaries是IndexedDiskCache(磁盘索引缓存)、TCPLateralCache(TCP横向缓存)、RemoteCacheServer(远程缓存服务器)。例如,磁盘缓存允许当你的内存达到阈值后把缓存对象交换到硬上。

第二步下载jcs

第三步,获得JCS依赖

第四步,配置JCS

JCS配置在一个叫"cache.ccf"的配置文件中,有替代品使用此文件,但他们超越了入门指南的范围。

缓存配置分为三部分:默认配置,regions配置和auxiliaries配置。你可以把auxiliaries想象为log4j的log4jappenders,regions是log4jcategories。每一个region都能指定auxiliaries。如果一个region不在Cache.ccf进行配置,则它将会使用默认的配置。以JCS和log4j不同的是JCS中预先定义的region不能使defaultregion内auxiliaries。

下面的cache.ccf文件定义了叫做”testCache1”的region,这个region使用了Indexed Disk Cache的axiliaries,它在这里被默认叫做DC。LRU内存缓存被选择用来管理内存
# DEFAULT CACHE REGION
jcs.default=DC
jcs.default.cacheattributes=
    org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=1000
jcs.default.cacheattributes.MemoryCacheName=
    org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.default.cacheattributes.UseMemoryShrinker=false
jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=3600
jcs.default.cacheattributes.ShrinkerIntervalSeconds=60
jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.default.elementattributes.IsEternal=false
jcs.default.elementattributes.MaxLifeSeconds=21600
jcs.default.elementattributes.IdleTime=1800
jcs.default.elementattributes.IsSpool=true
jcs.default.elementattributes.IsRemote=true
jcs.default.elementattributes.IsLateral=true

# PRE-DEFINED CACHE REGIONS
jcs.region.testCache1=DC
jcs.region.testCache1.cacheattributes=
    org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.testCache1.cacheattributes.MaxObjects=1000
jcs.region.testCache1.cacheattributes.MemoryCacheName=
    org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.testCache1.cacheattributes.UseMemoryShrinker=false
jcs.region.testCache1.cacheattributes.MaxMemoryIdleTimeSeconds=3600
jcs.region.testCache1.cacheattributes.ShrinkerIntervalSeconds=60
jcs.region.testCache1.cacheattributes.MaxSpoolPerRun=500
jcs.region.testCache1.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.testCache1.elementattributes.IsEternal=false

# AVAILABLE AUXILIARY CACHES
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=${user.dir}/jcs_swap
jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000000
jcs.auxiliary.DC.attributes.MaxKeySize=1000000
jcs.auxiliary.DC.attributes.MaxRecycleBinSize=5000
jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000
jcs.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 );
            }