Java 緩存系統(tǒng)(Java Caching System,JCS)
是一個(gè)用于 Java 應(yīng)用程序的強(qiáng)大分布式緩存系統(tǒng),它是擁有簡單 API 的高度可配置的工具。具體使用方法如下:
首先需要一個(gè)重要的配置文件, cache.ccf
基本配置:
jcs.default=jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=1000
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
該配置文件將內(nèi)存緩存指定為一個(gè)
LRUMemoryCache
。還可以看到,內(nèi)存中能保存的對象的最大數(shù)量被設(shè)置為 1000
。JCS 配置中定義的區(qū)域:
jcs.default=DISK_REGION
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.region.OUR_REGION=DISK_REGION
jcs.region.OUR_REGION.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.OUR_REGION.cacheattributes.MaxObjects=1000
jcs.region.OUR_REGION.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.OUR_REGION.cacheattributes.UseMemoryShrinker=true
jcs.region.OUR_REGION.cacheattributes.MaxMemoryIdleTimeSeconds=3600
jcs.region.OUR_REGION.cacheattributes.ShrinkerIntervalSeconds=60
jcs.region.OUR_REGION.cacheattributes.MaxSpoolPerRun=500
jcs.region.OUR_REGION.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.OUR_REGION.elementattributes.IsEternal=false
jcs.auxiliary.DISK_REGION=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory
jcs.auxiliary.DISK_REGION.attributes=org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes
jcs.auxiliary.DISK_REGION.attributes.DiskPath=c:/jcs/disk_region
jcs.auxiliary.DISK_REGION.attributes.maxKeySize=100000
第一行表明該配置將默認(rèn)區(qū)域設(shè)置為
DISK_REGION
。DISK_REGION
是 IndexedDiskCacheFactory
類型,并且該文件在磁盤上指定為 c:\jcs\disk_region。第二個(gè)配置組定義了我自己的區(qū)域,我為它添加了一些選項(xiàng),這種類型的配置(在指定用戶定義區(qū)域時(shí)同時(shí)使用內(nèi)存區(qū)域和磁盤區(qū)域)是很常見的。第 3 個(gè)配置組定義了一個(gè) 輔助區(qū)域 。
JCS 有兩個(gè)依賴項(xiàng):concurrent
和 commons-logging
(JCS 1.2.7.0 之前的版本中,還有兩個(gè)其他依賴項(xiàng):commons-collections
和 commons-lang
)。
JCS 的基本用法:
Initialize the JCS object and get an instance of the default cache region
JCS cache = JCS.getInstance("default");
JCS 實(shí)例后,可以調(diào)用最需要的方法。put
方法將一個(gè)新對象放入緩存中。接下來只需一個(gè) key
(第一個(gè)參數(shù))和一個(gè) value
(第二個(gè)參數(shù)):
cache.put(key, value);
檢索緩存對象只使用 JCS 提供的 get
方法:
cache.get(key);
清除緩存區(qū)域:
// Dispose of a specific cached item
cache.remove(key);
// Dispose of all cache data
cache.clear();
// Dispose of the cache region
cache.dispose();
在使用JCS時(shí)候, 我們經(jīng)常會(huì)把對象放入緩存中, 這里需要注意要實(shí)現(xiàn)序列化.
EHCache使用簡介
EHCache 是一個(gè)純java的,在Hibernate2.1充當(dāng)可插入的的在進(jìn)程中的緩存,它具有以下緩存,最小的依賴性,全面的文特性:快速,簡單,豐富的文檔和測試用例。
官方網(wǎng)站 http://ehcache.sourceforge.net/
在使用EhCache也需要一個(gè)配置文件, 但現(xiàn)在版本你也可以不需要配置文件, 在創(chuàng)建Cache時(shí)候傳一些配置值給它就可以了.
首先需要?jiǎng)?chuàng)建一個(gè)CacheManager 對象, 有四種方式:
方法一, 默認(rèn)配置文件創(chuàng)建:
CacheManager manager = CacheManager.create();
方法二, 使用指定配置文件創(chuàng)建:
CacheManager manager = CacheManager.create("src/config/ehcache.xml");
方法三, 從classpath找尋配置文件并創(chuàng)建:
URL url = getClass().getResource("/anotherconfigurationname.xml");
CacheManager manager = CacheManager.create(url);
方法四, 通過輸入流創(chuàng)建:
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
CacheManager manager = CacheManager.create(fis);
} finally {
fis.close();
}
卸載CacheManager ,關(guān)閉Cache
manager.shutdown();
使用Caches
取得配置文件中預(yù)先 定義的sampleCache1設(shè)置,生成一個(gè)Cache
Cache cache = manager.getCache("sampleCache1");
設(shè)置一個(gè)名為test 的新cache,test屬性為默認(rèn)
CacheManager manager = CacheManager.create();
manager.addCache("test");
設(shè)置一個(gè)名為test 的新cache,并定義其屬性
CacheManager manager = CacheManager.create();
Cache cache = new Cache("test", 1, true, false, 5, 2);
manager.addCache(cache);
往cache中加入元素
Element element = new Element("key1", "value1");
cache.put(new Element(element);
從cache中取得元素
Element element = cache.get("key1");
Cache 屬性說明:
構(gòu)造函數(shù):
public Cache(java.lang.String name, int maxElementsInMemory, boolean overflowToDisk, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds)
參數(shù)說明:
name - 元素名字。
maxElementsInMemory - 設(shè)定內(nèi)存中創(chuàng)建對象的最大值。
overflowToDisk - 設(shè)置當(dāng)內(nèi)存中緩存達(dá)到 maxInMemory 限制時(shí)元素是否可寫到磁盤上。
eternal - 設(shè)置元素(譯注:內(nèi)存中對象)是否永久駐留。如果是,將忽略超時(shí)限制且元素永不消亡。
timeToIdleSeconds - 設(shè)置某個(gè)元素消亡前的停頓時(shí)間。也就是在一個(gè)元素消亡之前,兩次訪問時(shí)間的最大時(shí)間間隔值。這只能在元素不是永久駐留時(shí)有效(譯注:如果對象永恒不滅,則 設(shè)置該屬性也無用)。 如果該值是 0 就意味著元素可以停頓無窮長的時(shí)間。
timeToLiveSeconds - 為元素設(shè)置消亡前的生存時(shí)間。也就是一個(gè)元素從構(gòu)建到消亡的最大時(shí)間間隔值。這只能在元素不是永久駐留時(shí)有效。
ehcache.xml配置文件中的屬性和構(gòu)造方法里面意思一樣.