开始:如果对JCS中的基本概念和术语不太清楚,请查阅相关资料先。
1. 首先看下类JCS的继承结构:
(1) CacheAccess类的注释中写到This class provides an interface for all types of access to the cache.,说明此类提供对缓存的操作接口。此类包含有两个成员:
l privatestatic CompositeCacheManagercacheMgr;
/**CachemanagerusebythevariousformsofdefineRegionandgetAccess*用来管理和获取region的CacheAccess对象
*/
CompositeCacheManager :用户管理各种cache域(Managesacompositecache.Thisprovidesaccesstocachesandistheprimarywaytoshutdownthe cachingsystemasawhole.)
l protected CompositeCache<K, V>cacheControl;
/**
*Thecachethatagiveninstanceofthisclassprovidesaccessto.
*
*这个是真正存储键值对的map,put和get操作的对象都是它
*/
CompositeCache是管理一个域的核心类(Thisistheprimaryhubforasinglecache/region.Itcontrolstheflowofitemsthroughthe cache.Theauxiliaryandmemorycachesarepluggedinhere.)。
(2) GroupCacheAccess:Access for groups.(不懂这个)
(3) JCS类
这是使用JCS系统的基本类,提供了对域的操作接口,一个缓存域应该对应一个JCS实例。
(4) JCS初始化
通过静态方法getInstance()得到一个实例。
/**
*GetaJCSwhichaccessestheprovidedregion.
*<p>
*@paramregionRegionthatreturnJCSwillprovideaccessto
*@returnAJCSwhichprovidesaccesstoagivenregion.
*@exceptionCacheException
*/
publicstatic <Kextends Serializable, Vextends Serializable> JCS<K, V> getInstance( String region )
throws CacheException
{
CompositeCache<K, V> cache =getCacheManager().getCache( region );
returnnew JCS<K, V>( cache );
}
首先通过getCacheManager()方法得到一个CompositeCacheManager实例,
protectedstatic CompositeCacheManager getCacheManager()throws CacheException
{
synchronized ( JCS.class )
{
if (cacheMgr ==null )
{
if (configProps !=null )
{
cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
cacheMgr.configure(configProps );
}
elseif (configFilename !=null )
{
cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
cacheMgr.configure(configFilename );
}
else
{
cacheMgr = CompositeCacheManager.getInstance();
}
}
returncacheMgr;
}
}
JCS的默认配置文件是cache.ccf,此文件应该位于类CompositeCacheManager的包下,同时JCS提供了两种自定义配置文件的方法setConfigFilename和setConfigProperties,两个定义在getCacheManager()中使用来初始化CompositeCacheManager,提供了两种定义配置文件的方法:Properties和文件路径,其中使用文件路径时应该注意,文件应该放在CompositeCacheManager类的包下面,这时直接使用文件名,或者放在src下面某个路径下,这时通过“/文件路径”访问(使用文件路径时是这样读取文件的InputStream is = getClass().getResourceAsStream( propFile ))。
下面分析加载配置文件的方法和得到cache实例的方法。