ehcache缓存的详细配置

用到缓存,主要是用来解决并发问题的。

 

其中ehcache是一个纯Java的过程中缓存实现Hibernate2.1,Spring都支持EHcache嵌入。

 

本文主要写Spring中引入ehcache而不是用hibernate.

 

ehcache部署起来很简单,主要分两步:

 

1.首先要给他写个核心配置XML文件

 

Xml代码   收藏代码
  1. <ehcache>  
  2.   
  3.                 <diskStore path="java.io.tmpdir"/>  
  4.   
  5.                 <defaultCache  
  6.                         maxElementsInMemory="10000"  
  7.                         eternal="false"  
  8.                         timeToIdleSeconds="120"  
  9.                         timeToLiveSeconds="120"  
  10.                         overflowToDisk="true"  
  11.                         diskPersistent="false"  
  12.                         diskExpiryThreadIntervalSeconds="120"  
  13.                         memoryStoreEvictionPolicy="LRU"  
  14.                         />  
  15.   
  16.                 <cache name="cache1"  
  17.                        maxElementsInMemory="10000"  
  18.                        eternal="false"      maxElementsOnDisk="1000"  
  19.                        overflowToDisk="true"  
  20.                        timeToIdleSeconds="300"  
  21.                        timeToLiveSeconds="600"  
  22.                        memoryStoreEvictionPolicy="LFU"  
  23.                         />  
  24.                           
  25.             </ehcache>  

 

 

属性解释:

 

简单配置,在ehcache.xml文件中有此配置,在使用Ehcache前最好将其删除掉,自己配置。

 

缓存名cache1,内存中最多可缓存10000个Element,其中的element会在闲置5分钟或是存活10分钟之后失效。

 

超过10000element时,element将会输出到磁盘中,输出路径是java.io.tmpdir。

 

 

从其他文章找到其详细解释:

 

·   Cache配置

 

·           name:Cache的唯一标识

·           maxElementsInMemory:内存中最大缓存对象数。

·           maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。

·           eternal:Element是否永久有效,一但设置了,timeout将不起作用。

·           overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。

·           timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。

·           timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。

·           diskPersistent:是否缓存虚拟机重启期数据。(这个虚拟机是指什么虚拟机一直没看明白是什么,有高人还希望能指点一二)。

·           diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。

·           diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。

·           memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。这里比较遗憾,Ehcache并没有提供一个用户定制策略的接口,仅仅支持三种指定策略,感觉做的不够理想。

 

 

2.实际要缓存的类调用

 

写一个实例类,这样大家就明白差不多了:

 

Java代码   收藏代码
  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.        public class Demo {  
  8.              
  9.            static CacheManager manager= new CacheManager();  
  10.   
  11.            /** 
  12.             *############################################################################## 
  13.             *  
  14.             * @DESCRIBE     
  15.             * @param args 
  16.             * @throws InterruptedException 
  17.             *                          
  18.             *############################################################################## 
  19.             */  
  20.            public static void main(String[] args) throws InterruptedException {  
  21.                  
  22.                String[] cacheNames = manager.getCacheNames();  
  23.                System.out.println("读取的缓存列表为:");  
  24.                for(int i=0;i<cacheNames.length;i++){   
  25.                    System.out.println("-- "+(i+1)+" "+cacheNames[i]);  
  26.                }  
  27.                  
  28.                Cache cache = manager.getCache("cache1");  
  29.                Element element = new Element("key1""value1");  
  30.                cache.put(element);  
  31.                  
  32.                element = cache.get("key1");  
  33.                Serializable value = element.getValue();  
  34.                System.out.println("序列化后的值为:"+value.toString());  
  35.   
  36.                element = cache.get("key1");  
  37.                Object value1 = element.getObjectValue();  
  38.                System.out.println("未序列化的值为:"+value1.toString());  
  39.                  
  40.                int elementsInMemory = cache.getSize();  
  41.                System.out.println("得到缓存的对象数量:"+elementsInMemory);  
  42.                  
  43.                long elementsInMemory1 = cache.getMemoryStoreSize();  
  44.                System.out.println("得到缓存对象占用内存的数量:"+elementsInMemory1);  
  45.                  
  46.                long elementsInMemory2 = cache.getDiskStoreSize();  
  47.                System.out.println("得到缓存对对象占用磁盘的数量:"+elementsInMemory2);          
  48.                  
  49.                int hits = cache.getHitCount();  
  50.                System.out.println("得到缓存读取的命中次数:"+hits);          
  51.                  
  52.                int hits1 = cache.getMemoryStoreHitCount();  
  53.                System.out.println("得到内存中缓存读取的命中次数:"+hits1);          
  54.                  
  55.                int hits2 =cache.getDiskStoreHitCount();  
  56.                System.out.println("得到磁盘中缓存读取的命中次数:"+hits2);          
  57.                  
  58.                int hits3 = cache.getMissCountNotFound();  
  59.                System.out.println("得到缓存读取的丢失次数:"+hits3);          
  60.                  
  61.                int hits4 = cache.getMissCountExpired();  
  62.                System.out.println("得到缓存读取的已经被销毁的对象丢失次数:"+hits4);      
  63.            }  
  64.   
  65.        }  

 

 

 

另:附件里需要的ehcache.jar包

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值