日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

使用ehcache

 CevenCheng 2012-04-18
  一直以來懶得配置緩存,基本的緩存也就是orm層,基本上都交給hibernate去配置了。這段時間,感覺頁面速度太慢了,還是需要使用緩存。現(xiàn)在的緩存工具也挺多的,較不錯的屬ehcache和oscache了。決定分別研究一下。
    先來說說ehcache,目前的版本為1.2,已經(jīng)支持集群了。對于ehcache的使用,感覺很容易上手,基本上都是配置。以前在hibernate的時候配置過,所以也不是很陌生。API也挺簡單,如下的api:
    CacheManager主要的緩存管理類,一般一個應(yīng)用為一個實例,如下
    CacheManager.create();也可以使用new CacheManager的方式創(chuàng)建
     默認(rèn)的配置文件為ehcache.xml文件,也可以使用不同的配置:
     
java 代碼
 
  1.    
  2. CacheManager manager = new CacheManager("src/config/other.xml");  
    

緩存的創(chuàng)建,采用自動的方式
java 代碼
 
  1.    
  2. CacheManager singletonManager = CacheManager.create();  
  3. singletonManager.addCache("testCache");  
  4. Cache test = singletonManager.getCache("testCache");      

或者直接創(chuàng)建Cache

java 代碼
 
  1.    
  2. CacheManager singletonManager = CacheManager.create();  
  3. Cache memoryOnlyCache = new Cache("testCache"5000falsefalse52);  
  4. manager.addCache(memoryOnlyCache);  
  5. Cache test = singletonManager.getCache("testCache");      

刪除cache
 
java 代碼
 
  1. CacheManager singletonManager = CacheManager.create();  
  2. singletonManager.removeCache("sampleCache1");    
  

在使用ehcache后,需要關(guān)閉
 
java 代碼
 
  1. CacheManager.getInstance().shutdown()      
  2.   
  3. caches 的使用  
  4.    
  5. Cache cache = manager.getCache("sampleCache1");       


執(zhí)行crud操作

 
java 代碼
 
  1. Cache cache = manager.getCache("sampleCache1");  
  2. Element element = new Element("key1""value1");  
  3. cache.put(element);      
  4.   
  5. update  
  6.    
  7. Cache cache = manager.getCache("sampleCache1");  
  8. cache.put(new Element("key1""value1");  
  9. //This updates the entry for "key1"  
  10. cache.put(new Element("key1""value2");      
  11.   
  12. get Serializable  
  13.    
  14. Cache cache = manager.getCache("sampleCache1");  
  15. Element element = cache.get("key1");  
  16. Serializable value = element.getValue();      
  17.   
  18. get non serializable  
  19.    
  20. Cache cache = manager.getCache("sampleCache1");  
  21. Element element = cache.get("key1");  
  22. Object value = element.getObjectValue();      
  23.   
  24. remove  
  25.    
  26. Cache cache = manager.getCache("sampleCache1");  
  27. Element element = new Element("key1""value1"  
  28. cache.remove("key1");      


不過緩存還是基本上以配置方式為主,下一篇文章將會說明ehcache如何配置
    

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多