EHcache缓存框架详解
生活随笔
收集整理的這篇文章主要介紹了
EHcache缓存框架详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
EhCache是一個純Java的進程內緩存框架,具有快速、精干等特點,也是Hibernate中默認的CacheProvider。
歸納一下它大概具有一下幾個特點:
1. 快速.
2. 簡單.
3. 多種緩存策略
4. 緩存數據有兩級:內存和磁盤,因此無需擔心容量問題
5. 緩存數據會在虛擬機重啟的過程中寫入磁盤
6. 可以通過RMI、可插入API等方式進行分布式緩存
7. 具有緩存和緩存管理器的偵聽接口
8. 支持多緩存管理器實例,以及一個實例的多個緩存區域
9. 提供Hibernate的緩存實現
那么我們在開發中到底如何運用EhCache框架呢?
獲取Ehcache相關jar包及幫助文檔。
下載地址:http://ehcache.org/code
相關文檔地址:http://ehcache.org/apidocs/
/*** maxElementsInMemory:緩存中允許創建的最大對象數* eternal:緩存中對象是否為永久的,如果是,超時設置將被忽略,對象從不過期。* timeToIdleSeconds:緩存數據的鈍化時間,也就是在一個元素消亡之前,兩次訪問時間的最大時間間隔值, 這只能在元素不是永久駐留時有效,* 如果該值是 0 就意味著元素可以停頓無窮長的時間。* timeToLiveSeconds:緩存數據的生存時間,也就是一個元素從構建到消亡的最大時間間隔值,這只能在元素不是永久駐留時有效,* 如果該值是0就意味著元素可以停頓無窮長的時間。 overflowToDisk:內存不足時,是否啟用磁盤緩存。* memoryStoreEvictionPolicy:緩存滿了之后的淘汰算法** @param args*/public static void main(String[] args) {// 創建一個緩存管理器對象CacheManager cacheManager = CacheManager.create();// 命名緩存管理器cacheManager.setName("testCacheManager");// 創建一個指定緩存名稱的緩存對象Cache cache = new Cache("testCache", 4, false, false, 1, 1);// cache.setDisabled(true);// 將緩存對象添加至緩存管理器cacheManager.addCache(cache);// cacheManager.shutdown();System.out.println("判斷緩存管理器中是否存在指定的緩存對象:"+ cacheManager.cacheExists("testCache"));DiskStorePathManager disStoreManager = cacheManager.getDiskStorePathManager();System.out.println("獲取當前配置文件硬盤路徑:"+ disStoreManager.getFile("testCache.xml"));Map<String, Object> map = new HashMap<String, Object>();map.put("name", "tom");map.put("sex", "男");map.put("age", 1);// 注意:如果當前緩存對象設置了內存中最大緩存keyValue對象的話,如果超出時,則后面的覆蓋前面的keyValue對象cache.put(new Element("cache1", map));cache.put(new Element("cache2", map));cache.put(new Element("cache3", map));Element element = new Element("cache4", map);element.setTimeToLive(1);cache.put(element);String[] cacheNames = cacheManager.getCacheNames();for (int i = 0; i < cacheNames.length; i++) {System.out.println("緩存" + i + ":" + cacheNames[i]);}// System.out.println("當前活動的緩存配置文件內容:\n"// + cacheManager.getActiveConfigurationText());System.out.println("緩存管理器對象是否命名:" + cacheManager.isNamed());Cache testCahe = cacheManager.getCache("testCache");System.out.println("緩存的狀態:" + testCahe.getStatus());System.out.println("緩存對象平均獲取時間:" + testCahe.getAverageGetTime());System.out.println("獲取緩存對象占用內存空間大小:" + testCahe.getMemoryStoreSize());System.out.println("獲取緩存對象大小:" + testCahe.getSize());System.out.println("緩存是否關閉:" + testCahe.isDisabled());System.out.println("判斷某一個緩存key是否存在在緩存中"+ testCahe.isKeyInCache("cache3"));System.out.println("判斷某一個緩存值是否緩存在對象中:" + testCahe.isValueInCache(map));// 驗證緩存對象是否禁用if (!testCahe.isDisabled()) {System.out.println("判斷緩存中某個對象是否過期:"+ testCahe.isExpired(testCahe.get("cache3")));} else {System.out.println(testCahe.getName() + "緩存已關閉");}System.out.println("判斷某一個key是否緩存在內存中:"+ testCahe.isElementInMemory("cache1"));System.out.println("判斷某一個key是否緩存在磁盤中:"+ testCahe.isElementOnDisk("cache1"));System.out.println("\n");List cacheKey = cache.getKeys();for (int i = 0; i < cacheKey.size(); i++) {Element cacheElement = testCahe.get(cacheKey.get(i));System.out.println("Key:" + cacheKey.get(i) + ",value:"+ cacheElement.getObjectValue());}}?
轉載于:https://www.cnblogs.com/java-chen/archive/2013/06/13/3133769.html
總結
以上是生活随笔為你收集整理的EHcache缓存框架详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于visual Studio2013解
- 下一篇: 更改数据库排序规则