Java 緩衝技術之 ehcache

來源:互聯網
上載者:User

標籤:

1. EHCache 的特點,是一個純Java ,過程中(也可以理解成插入式)緩衝實現,單獨安裝Ehcache ,需把ehcache-X.X.jar 和相關類庫方到classpath中。如項目已安裝了Hibernate ,則不需要做什麼。。直接可以使用Ehcache 

Cache 儲存方式 :記憶體或磁碟 

2. 單獨使用 EHCache 

 

所以大概步驟為: 
第一步:產生CacheManager對象 
第二步:產生Cache對象 
第三步:向Cache對象裡添加由key,value組成的索引值對的Element元素 

具體一個Test.java程式: 

 
  1. package test;  
  2.   
  3. import net.sf.ehcache.Cache;  
  4. import net.sf.ehcache.CacheManager;  
  5. import net.sf.ehcache.Element;  
  6. /** 
  7.  * 第一步:產生CacheManager對象 
  8.  * 第二步:產生Cache對象 
  9.  * 第三步:向Cache對象裡添加由key,value組成的索引值對的Element元素 
  10.  * @author mahaibo 
  11.  * 
  12.  */  
  13. public class Test {  
  14.       
  15.     public static void main(String[] args) {  
  16.           //指定ehcache.xml的位置  
  17.           String fileName="E:\\1008\\workspace\\ehcachetest\\ehcache.xml";  
  18.           CacheManager manager = new CacheManager(fileName);  
  19.           //取出所有的cacheName  
  20.           String names[] = manager.getCacheNames();  
  21.           for(int i=0;i<names.length;i++){  
  22.               System.out.println(names[i]);  
  23.           }  
  24.           //根據cacheName產生一個Cache對象  
  25.           //第一種方式:  
  26.           Cache cache=manager.getCache(names[0]);  
  27.             
  28.           //第二種方式,ehcache裡必須有defaultCache存在,"test"可以換成任何值  
  29. //        Cache cache = new Cache("test", 1, true, false, 5, 2);     
  30. //        manager.addCache(cache);   
  31.             
  32.           //向Cache對象裡添加Element元素,Element元素有key,value索引值對組成  
  33.           cache.put(new Element("key1","values1"));  
  34.           Element element = cache.get("key1");  
  35.             
  36.           System.out.println(element.getValue());  
  37.           Object obj = element.getObjectValue();  
  38.           System.out.println((String)obj);  
  39.           manager.shutdown();  
  40.             
  41.   
  42.      }  
  43.   
  44.   
  45. }  

 

Java 緩衝技術之 ehcache

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.