最近項目中需要使用Cache來控制程序運行中占用內存大小, 搜索了一下, JCS和Ehcache使用比較簡單.
Java 緩存系統(Java Caching System,JCS)
是一個用于
Java 應用程序的強大分布式緩存系統,它是擁有簡單 API 的高度可配置的工具。具體使用方法如下:
首先需要一個重要的配置文件, 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
該配置文件將內存緩存指定為一個
LRUMemoryCache
。還可以看到,內存中能保存的對象的最大數量被設置為
1000
。
JCS 配置中定義的區域:
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
第一行表明該配置將默認區域設置為
DISK_REGION
。
DISK_REGION
是
IndexedDiskCacheFactory
類型,并且該文件在磁盤上指定為 c:\jcs\disk_region。第二個配置組定義了我自己的區域,我為它添加了一些選項,這種類型的配置(在指定用戶定義區域時同時使用內存區域和磁盤區域)是很常見的。第 3 個配置組定義了一個 輔助區域 。
JCS 有兩個依賴項:concurrent
和 commons-logging
(JCS 1.2.7.0 之前的版本中,還有兩個其他依賴項:commons-collections
和 commons-lang
)。
JCS 的基本用法:
Initialize the JCS object and get an instance of the default cache region
JCS cache = JCS.getInstance("default");
JCS 實例后,可以調用最需要的方法。put
方法將一個新對象放入緩存中。接下來只需一個 key
(第一個參數)和一個 value
(第二個參數):
cache.put(key, value);
檢索緩存對象只使用 JCS 提供的 get
方法:
cache.get(key);
清除緩存區域:
// Dispose of a specific cached item
cache.remove(key);
// Dispose of all cache data
cache.clear();
// Dispose of the cache region
cache.dispose();
在使用JCS時候, 我們經常會把對象放入緩存中, 這里需要注意要實現序列化.
EHCache使用簡介
EHCache 是一個純java的,在Hibernate2.1充當可插入的的在進程中的緩存,它具有以下緩存,最小的依賴性,全面的文特性:快速,簡單,豐富的文檔和測試用例。
官方網站 http://ehcache.sourceforge.net/
在使用EhCache也需要一個配置文件, 但現在版本你也可以不需要配置文件, 在創建Cache時候傳一些配置值給它就可以了.
首先需要創建一個CacheManager 對象, 有四種方式:
方法一, 默認配置文件創建:
CacheManager manager = CacheManager.create();
方法二, 使用指定配置文件創建:
CacheManager manager = CacheManager.create("src/config/ehcache.xml");
方法三, 從classpath找尋配置文件并創建:
URL url = getClass().getResource("/anotherconfigurationname.xml");
CacheManager manager = CacheManager.create(url);
方法四, 通過輸入流創建:
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
CacheManager manager = CacheManager.create(fis);
} finally {
fis.close();
}
卸載CacheManager ,關閉Cache
manager.shutdown();
使用Caches
取得配置文件中預先 定義的sampleCache1設置,生成一個Cache
Cache cache = manager.getCache("sampleCache1");
設置一個名為test 的新cache,test屬性為默認
CacheManager manager = CacheManager.create();
manager.addCache("test");
設置一個名為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 屬性說明:
構造函數:
public Cache(java.lang.String name, int maxElementsInMemory, boolean overflowToDisk, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds)
參數說明:
name - 元素名字。
maxElementsInMemory - 設定內存中創建對象的最大值。
overflowToDisk - 設置當內存中緩存達到 maxInMemory 限制時元素是否可寫到磁盤上。
eternal - 設置元素(譯注:內存中對象)是否永久駐留。如果是,將忽略超時限制且元素永不消亡。
timeToIdleSeconds - 設置某個元素消亡前的停頓時間。也就是在一個元素消亡之前,兩次訪問時間的最大時間間隔值。這只能在元素不是永久駐留時有效(譯注:如果對象永恒不滅,則 設置該屬性也無用)。 如果該值是 0 就意味著元素可以停頓無窮長的時間。
timeToLiveSeconds - 為元素設置消亡前的生存時間。也就是一個元素從構建到消亡的最大時間間隔值。這只能在元素不是永久駐留時有效。
ehcache.xml配置文件中的屬性和構造方法里面意思一樣.
posted on 2009-05-13 22:28
周銳 閱讀(1341)
評論(0) 編輯 收藏 所屬分類:
Java