Ehcache简单使用二

Ehcache是Java虚拟机级的缓存,其可以用于许多地方。如:被Hibernate用作二级缓存,Web页缓存、Web页片段缓存、Session同步等。

官方主页:http://ehcache.org/

 

主要测试类:

[java]  view plain copy
  1. package my.test.ehcache1;  
  2.   
  3. import java.io.InputStream;  
  4. import java.lang.management.ManagementFactory;  
  5.   
  6. import javax.management.MBeanServer;  
  7.   
  8. import net.sf.ehcache.Cache;  
  9. import net.sf.ehcache.CacheManager;  
  10. import net.sf.ehcache.Element;  
  11. import net.sf.ehcache.management.ManagementService;  
  12.   
  13. public class EHCacheTest {  
  14.   
  15.     public static void main(String[] args) {  
  16.         InputStream is = EHCacheTest.class  
  17.                 .getResourceAsStream("/my/test/ehcache1/ehcache.xml");  
  18.         //读入配置  
  19.         CacheManager cacheManager = new CacheManager(is);  
  20.         //打印初始缓存  
  21.         String[] cacheNames = cacheManager.getCacheNames();  
  22.         printNames(cacheNames);  
  23.         //移除缓存  
  24.         cacheManager.removeCache("sampleDistributedCache1");  
  25.         cacheNames = cacheManager.getCacheNames();  
  26.         printNames(cacheNames);  
  27.   
  28.         //新建缓存  
  29.         Cache cache = new Cache("Test1", 100, true, false, 10, 10);  
  30.         cacheManager.addCache(cache);  
  31.         cacheNames = cacheManager.getCacheNames();  
  32.         printNames(cacheNames);  
  33.         cache.put(new Element("test1", "value1"));  
  34.           
  35.         //得到缓存并插入值(这里监听器被调用)  
  36.         cache = cacheManager.getCache("sampleCache3");  
  37.         for (int i = 0; i < 20; i++) {  
  38.             cache.put(new Element("key" + i, "value" + i));  
  39.         }  
  40.         cache.get("key10");  
  41.           
  42.         // distributed -- rmi同步  
  43.         cache = cacheManager.getCache("sampleDistributedCache2");  
  44.         for (int i = 0; i < 100; i++) {  
  45.             cache.put(new Element("key" + i , "value" + i));  
  46.         }  
  47.           
  48.         //注册被管理的Bean  
  49.         // JMX -- jconsole(MBeanServer)  
  50.         MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();  
  51.         ManagementService.registerMBeans(cacheManager, mBeanServer, true, true,  
  52.                 true, true);  
  53.   
  54.         for (int i = 0; i < 10; i++) {  
  55.             Element temp = cache.get("ehcache");  
  56.             if (temp != null) {  
  57.                 System.out.println(temp.getValue());  
  58.             } else {  
  59.                 System.out.println("NotFound");  
  60.             }  
  61.             try {  
  62.                 Thread.sleep(10000);  
  63.             } catch (InterruptedException e) {  
  64.                 e.printStackTrace();  
  65.             }  
  66.         }  
  67.     }  
  68.   
  69.     private static void printNames(String[] names) {  
  70.         System.out.println("=======================");  
  71.         for (int i = 0; i < names.length; i++) {  
  72.             System.out.println(names[i]);  
  73.         }  
  74.     }  
  75.   
  76. }  

 

Cache事件监听器工厂类:

[java]  view plain copy
  1. package my.test.ehcache1;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import net.sf.ehcache.event.CacheEventListener;  
  6. import net.sf.ehcache.event.CacheEventListenerFactory;  
  7.   
  8. public class CacheEvtLstnFac extends CacheEventListenerFactory {  
  9.   
  10.     @Override  
  11.     public CacheEventListener createCacheEventListener(Properties arg0) {  
  12.         return new CacheEvtLstn();  
  13.     }  
  14.   
  15. }  

 

Cache事件监听器类:

[java]  view plain copy
  1. package my.test.ehcache1;  
  2.   
  3. import net.sf.ehcache.CacheException;  
  4. import net.sf.ehcache.Ehcache;  
  5. import net.sf.ehcache.Element;  
  6. import net.sf.ehcache.event.CacheEventListener;  
  7.   
  8. public class CacheEvtLstn implements CacheEventListener {  
  9.       
  10.     public Object clone() {  
  11.         return this;  
  12.     }  
  13.       
  14.     public void dispose() {  
  15.         // TODO Auto-generated method stub  
  16.   
  17.     }  
  18.   
  19.     public void notifyElementEvicted(Ehcache arg0, Element arg1) {  
  20.         System.out.println(arg1.getKey() + "=" + arg1.getValue()  
  21.                 + " has been evicted");  
  22.     }  
  23.   
  24.     public void notifyElementExpired(Ehcache arg0, Element arg1) {  
  25.         // TODO Auto-generated method stub  
  26.   
  27.     }  
  28.   
  29.     public void notifyElementPut(Ehcache arg0, Element arg1)  
  30.             throws CacheException {  
  31.         System.out.println(arg1.getKey() + "=" + arg1.getValue()  
  32.                 + " has been added");  
  33.     }  
  34.   
  35.     public void notifyElementRemoved(Ehcache arg0, Element arg1)  
  36.             throws CacheException {  
  37.         // TODO Auto-generated method stub  
  38.   
  39.     }  
  40.   
  41.     public void notifyElementUpdated(Ehcache arg0, Element arg1)  
  42.             throws CacheException {  
  43.         // TODO Auto-generated method stub  
  44.   
  45.     }  
  46.   
  47.     public void notifyRemoveAll(Ehcache arg0) {  
  48.         // TODO Auto-generated method stub  
  49.   
  50.     }  
  51.   
  52. }  

 

xml配置文件:更改官方包中的样例文件ehcache.xml中的sampleCache3为如下形式:

[xhtml]  view plain copy
  1. <cache name="sampleCache3"  
  2.            maxElementsInMemory="10"  
  3.            eternal="false"  
  4.            overflowToDisk="false"  
  5.            timeToIdleSeconds="300"  
  6.            timeToLiveSeconds="600"  
  7.            diskPersistent="false"  
  8.            diskExpiryThreadIntervalSeconds="1"  
  9.            memoryStoreEvictionPolicy="LFU">  
  10.            <cacheEventListenerFactory class="my.test.ehcache1.CacheEvtLstnFac" properties=""/>  
  11.      </cache>  

 

输出结果:

2009-12-13 11:36:02 net.sf.ehcache.distribution.RMICacheManagerPeerListener <init>
警告: Explicitly setting the listener hostname to 'localhost' is not recommended. It will only work if all CacheManager peers are on the same machine.
=======================
sampleDistributedCache2
sampleCache2
sampleCache1
sample/DistributedCache3
sampleDistributedCache1
sampleCache3
=======================
sampleDistributedCache2
sampleCache2
sampleCache1
sample/DistributedCache3
sampleCache3
=======================
sampleDistributedCache2
sampleCache2
Test1
sampleCache1
sample/DistributedCache3
sampleCache3
key0=value0 has been added
key1=value1 has been added
key2=value2 has been added
key3=value3 has been added
key4=value4 has been added
key5=value5 has been added
key6=value6 has been added
key7=value7 has been added
key8=value8 has been added
key9=value9 has been added
key0=value0 has been evicted
key10=value10 has been added
key2=value2 has been evicted
key11=value11 has been added
key1=value1 has been evicted
key12=value12 has been added
key4=value4 has been evicted
key13=value13 has been added
key9=value9 has been evicted
key14=value14 has been added
key7=value7 has been evicted
key15=value15 has been added
key5=value5 has been evicted
key16=value16 has been added
key14=value14 has been evicted
key17=value17 has been added
key12=value12 has been evicted
key18=value18 has been added
key8=value8 has been evicted
key19=value19 has been added
NotFound

 

解释:

首先打印配置文件中的cache名

接着移除了名为“sampleDistributedCache1”的cache,再打印cache名

再添加"Test1"cache,再打印cache名

接着向sampleCache3插入值,因为这个注册了监听器,所以监听器的会输出相应信息

这里配置了maxElementsInMemory="10",overflowToDisk="false",表明最多只能存10个条目在内存中,并且不能使用磁盘,因此从10-19添加的条目,会将之前存储的条目从缓存中移除。

 

由于将CacheManager注册到了本地管理BeanServer,所以可以通过jdk的jconsole命令查看管理Bean,如图:

Ehcache简单使用二_第1张图片

版权

你可能感兴趣的:(Ehcache简单使用二)