`
kong0itey
  • 浏览: 299062 次
社区版块
存档分类
最新评论

ehcache缓存管理

    博客分类:
  • java
 
阅读更多

EHCache 是一个纯java的,在Hibernate2.1充当可插入的的在进程中的缓存,它具有以下缓存,最小的依赖性,全面的文特性:快速,简单,丰富的文档和测试用例。

     官方网站 http://ehcache.sourceforge.net/  


     ehcache-1.2 cacheNames 列表的取得;

     方法一:

  1. CacheManager.create();   
  2. String[] cacheNames = CacheManager.getInstance().getCacheNames();  

     方法二:

  1. CacheManager manager =  new  CacheManager();   
  2. String[] cacheNames = manager.getCacheNames();  

     方法三:

  1. CacheManager manager1 =  new  CacheManager( 'src/config/ehcache1.' );   
  2. CacheManager manager2 =  new  CacheManager( 'src/config/ehcache2.xml' );   
  3. String[] cacheNamesForManager1 = manager1.getCacheNames();   
  4. String[] cacheNamesForManager2 = manager2.getCacheNames();  


     ehcache-1.2 管理器各种建立的方法:

     方法一:

  1. CacheManager manager =  new  CacheManager();  

     方法二:

  1. CacheManager manager =  new  CacheManager( 'src/config/ehcache.xml' );  

     方法三:

  1. URL url = getClass().getResource( '/anotherconfigurationname.xml' );   
  2. CacheManager manager =  new  CacheManager(url);  

     方法四:

  1. InputStream fis =  new  FileInputStream( new  File( 'src/config/ehcache.xml' ).getAbsolutePath());   
  2. try  {   
  3.     CacheManager manager =  new  CacheManager(fis);   
  4. finally  {   
  5.     fis.close();   
  6. }  


     添加和删除缓存元素

         设置一个名为test 的新cache,test属性为默认

  1. CacheManager singletonManager = CacheManager.create();   
  2. singletonManager.addCache( 'testCache' );   
  3. Cache test = singletonManager.getCache( 'testCache' );  

         设置一个名为test 的新cache,并定义其属性 

  1. CacheManager singletonManager = CacheManager.create();   
  2. Cache memoryOnlyCache =  new  Cache( 'testCache' 5000 false false 5 2 );   
  3. manager.addCache(memoryOnlyCache);   
  4. Cache test = singletonManager.getCache( 'testCache' );  


        
         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              - 为元素设置消亡前的生存时间。
                                                        也就是一个元素从构建到消亡的最大时间间隔值。
                                                        这只能在元素不是永久驻留时有效。

         删除缓存元素:

  1. CacheManager singletonManager = CacheManager.create();   
  2. singletonManager.removeCache( 'test' );  


     关闭缓存管理器 CacheManager

  1. CacheManager.getInstance().shutdown();  


     对于缓存对象的操作:
         放入一个简单的对象到缓存元素;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. Element element =  new  Element( 'key1' 'value1' );   
  3. cache.put(element);  

         得到一个序列化后的对象属性值;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. Element element = cache.get( 'key1' );   
  3. Serializable value = element.getValue();  

         得到一个没有序列化后的对象属性值;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. Element element = cache.get( 'key1' );   
  3. Object value = element.getObjectValue();  

         删除一个对象从元素;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. Element element =  new  Element( 'key1' 'value1'   
  3. cache.remove( 'key1' );  

     对于永固性磁盘存储,立即存储到磁盘:

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. cache.flush();  


     获得缓存大小:
         得到缓存的对象数量;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. int  elementsInMemory = cache.getSize();  

         得到缓存对象占用内存的数量

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. long  elementsInMemory = cache.getMemoryStoreSize();  

         得到缓存对对象占用磁盘的数量

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. long  elementsInMemory = cache.getDiskStoreSize();  

     关于缓存的读取和丢失的记录
         得到缓存读取的命中次数;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. int  hits = cache.getHitCount();  

         得到内存中缓存读取的命中次数;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. int  hits = cache.getMemoryStoreHitCount();  

         得到磁盘中缓存读取的命中次数;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. int  hits = cache.getDiskStoreCount();  

          得到缓存读取的丢失次数;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. int  hits = cache.getMissCountNotFound();  

         得到缓存读取的已经被销毁的对象丢失次数;

  1. Cache cache = manager.getCache( 'sampleCache1' );   
  2. int  hits = cache.getMissCountExpired();  

--------------------------
----------简单例子------------
--------------------------

     实战:
         XML文件格式:

  1.   maxElementsInMemory = '10000'   
  2.   eternal = 'false'   
  3.   timeToIdleSeconds = '120'   
  4.   timeToLiveSeconds = '120'   
  5.   overflowToDisk = 'true'   
  6.   diskPersistent = 'false'   
  7.   diskExpiryThreadIntervalSeconds = '120'   
  8.   memoryStoreEvictionPolicy = 'LRU'   
  9.   />   
  10.               maxElementsInMemory = '10000'   
  11. eternal = 'false'   
  12. overflowToDisk = 'true'   
  13. timeToIdleSeconds = '2'   
  14. timeToLiveSeconds = '3'   
  15. memoryStoreEvictionPolicy = 'LFU'   
  16.   />   


     源码:

  1. import  java.io.Serializable;   
  2.   
  3. import  net.sf.ehcache.Cache;   
  4. import  net.sf.ehcache.CacheManager;   
  5. import  net.sf.ehcache.Element;   
  6.   
  7. /**  
  8.  #############################################################################  
  9.  # DESCRIBE ehcache 缓存操作DEMO  
  10.  # AUTHOR    悠~游  
  11.  # DATE      2006-7-10  
  12.  # COMPANY   FLX  
  13.  # PORJECT   ehcache-demo  
  14.  #############################################################################  
  15.  */   
  16.   
  17. public   class  Demo {   
  18.       
  19.      static  CacheManager manager=  new  CacheManager();   
  20.   
  21.      /**  
  22.      *##############################################################################  
  23.      *   
  24.      * @DESCRIBE      
  25.      * @param args  
  26.      * @throws InterruptedException  
  27.      *                           
  28.      *##############################################################################  
  29.      */   
  30.      public   static   void  main(String[] args)  throws  InterruptedException {   
  31.           
  32.         String[] cacheNames = manager.getCacheNames();   
  33.         System.out.println( '读取的缓存列表为:' );   
  34.          for ( int  i= 0 ;i                     System.out.println( '-- ' +(i+ 1 )+ ' ' +cacheNames[i]);   
  35.         }   
  36.           
  37.         Cache cache = manager.getCache( 'cache1' );   
  38.         Element element =  new  Element( 'key1' 'value1' );   
  39.         cache.put(element);   
  40.           
  41.         element = cache.get( 'key1' );   
  42.         Serializable value = element.getValue();   
  43.         System.out.println( '序列化后的值为:' +value.toString());   
  44.   
  45.         element = cache.get( 'key1' );   
  46.         Object value1 = element.getObjectValue();   
  47.         System.out.println( '未序列化的值为:' +value1.toString());   
  48.           
  49.          int  elementsInMemory = cache.getSize();   
  50.         System.out.println( '得到缓存的对象数量:' +elementsInMemory);   
  51.           
  52.          long  elementsInMemory1 = cache.getMemoryStoreSize();   
  53.         System.out.println( '得到缓存对象占用内存的数量:' +elementsInMemory1);   
  54.           
  55.          long  elementsInMemory2 = cache.getDiskStoreSize();   
  56.         System.out.println( '得到缓存对对象占用磁盘的数量:' +elementsInMemory2);           
  57.           
  58.          int  hits = cache.getHitCount();   
  59.         System.out.println( '得到缓存读取的命中次数:' +hits);           
  60.           
  61.          int  hits1 = cache.getMemoryStoreHitCount();   
  62.         System.out.println( '得到内存中缓存读取的命中次数:' +hits1);           
  63.           
  64.          int  hits2 =cache.getDiskStoreHitCount();   
  65.         System.out.println( '得到磁盘中缓存读取的命中次数:' +hits2);           
  66.           
  67.          int  hits3 = cache.getMissCountNotFound();   
  68.         System.out.println( '得到缓存读取的丢失次数:' +hits3);           
  69.           
  70.          int  hits4 = cache.getMissCountExpired();   
  71.         System.out.println( '得到缓存读取的已经被销毁的对象丢失次数:' +hits4);       
  72.     }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics