Ehcache是Java虚拟机级的缓存,其可以用于许多地方。如:被Hibernate用作二级缓存,Web页缓存、Web页片段缓存、Session同步等。
官方主页:http://ehcache.org/
主要测试类:
package my.test.ehcache1; import java.io.InputStream; import java.lang.management.ManagementFactory; import javax.management.MBeanServer; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sf.ehcache.management.ManagementService; public class EHCacheTest { public static void main(String[] args) { InputStream is = EHCacheTest.class .getResourceAsStream("/my/test/ehcache1/ehcache.xml"); //读入配置 CacheManager cacheManager = new CacheManager(is); //打印初始缓存 String[] cacheNames = cacheManager.getCacheNames(); printNames(cacheNames); //移除缓存 cacheManager.removeCache("sampleDistributedCache1"); cacheNames = cacheManager.getCacheNames(); printNames(cacheNames); //新建缓存 Cache cache = new Cache("Test1", 100, true, false, 10, 10); cacheManager.addCache(cache); cacheNames = cacheManager.getCacheNames(); printNames(cacheNames); cache.put(new Element("test1", "value1")); //得到缓存并插入值(这里监听器被调用) cache = cacheManager.getCache("sampleCache3"); for (int i = 0; i < 20; i++) { cache.put(new Element("key" + i, "value" + i)); } cache.get("key10"); // distributed -- rmi同步 cache = cacheManager.getCache("sampleDistributedCache2"); for (int i = 0; i < 100; i++) { cache.put(new Element("key" + i , "value" + i)); } //注册被管理的Bean // JMX -- jconsole(MBeanServer) MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true); for (int i = 0; i < 10; i++) { Element temp = cache.get("ehcache"); if (temp != null) { System.out.println(temp.getValue()); } else { System.out.println("NotFound"); } try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } } private static void printNames(String[] names) { System.out.println("======================="); for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } } }
Cache事件监听器工厂类:
package my.test.ehcache1; import java.util.Properties; import net.sf.ehcache.event.CacheEventListener; import net.sf.ehcache.event.CacheEventListenerFactory; public class CacheEvtLstnFac extends CacheEventListenerFactory { @Override public CacheEventListener createCacheEventListener(Properties arg0) { return new CacheEvtLstn(); } }
Cache事件监听器类:
package my.test.ehcache1; import net.sf.ehcache.CacheException; import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; import net.sf.ehcache.event.CacheEventListener; public class CacheEvtLstn implements CacheEventListener { public Object clone() { return this; } public void dispose() { // TODO Auto-generated method stub } public void notifyElementEvicted(Ehcache arg0, Element arg1) { System.out.println(arg1.getKey() + "=" + arg1.getValue() + " has been evicted"); } public void notifyElementExpired(Ehcache arg0, Element arg1) { // TODO Auto-generated method stub } public void notifyElementPut(Ehcache arg0, Element arg1) throws CacheException { System.out.println(arg1.getKey() + "=" + arg1.getValue() + " has been added"); } public void notifyElementRemoved(Ehcache arg0, Element arg1) throws CacheException { // TODO Auto-generated method stub } public void notifyElementUpdated(Ehcache arg0, Element arg1) throws CacheException { // TODO Auto-generated method stub } public void notifyRemoveAll(Ehcache arg0) { // TODO Auto-generated method stub } }
xml配置文件:更改官方包中的样例文件ehcache.xml中的sampleCache3为如下形式:
<cache name="sampleCache3" maxElementsInMemory="10" eternal="false" overflowToDisk="false" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false" diskExpiryThreadIntervalSeconds="1" memoryStoreEvictionPolicy="LFU"> <cacheEventListenerFactory class="my.test.ehcache1.CacheEvtLstnFac" properties=""/> </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,如图: