ehCache浅谈

来源:互联网 发布:p城堡垒优化助手 编辑:程序博客网 时间:2024/04/30 06:28
ehCache浅谈 2011-09-14 12:59:39

分类: 服务器与存储

Ehcache的类层次模型主要为三层,最上层的是CacheManager,他是操作Ehcache的入口。我们可以通过CacheManager.getInstance()获得一个单子的CacheManger,或者通过CacheManger的构造函数创建一个新的CacheManger。每个CacheManager都管理着多个Cache。而每个Cache都以一种类Hash的方式,关联着多个Element。而Element则是我们用于存放要缓存内容的地方。

在配置EhCache前需要引入两个开发包:ehcache-1.3.0.jar和commons-logging-1.04.jar

配置文件

例子:ehcache.xml

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <ehcache>



  3.     <defaultCache maxElementsInMemory="2" eternal="false"

  4.         timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="false"

  5.         memoryStoreEvictionPolicy="LRU" />



  6.     <cache name="sampleCache1" maxElementsInMemory="5" eternal="false"

  7.         overflowToDisk="false" timeToIdleSeconds="1" timeToLiveSeconds="1"

  8.         memoryStoreEvictionPolicy="LRU" >

  9.      </cache>

  10. </ehcache>

注:在ehcache的配置文件里面必须配置defaultCache。每个标签定义一个新的cache,属性的含义基本上可以从名字上得到,详细的说明可以参考上面的链接。

示例程序:

例子:

  1. package ehcache.test;

  2. import java.util.List;

  3. import net.sf.ehcache.Cache;
  4. import net.sf.ehcache.CacheManager;
  5. import net.sf.ehcache.Element;

  6. public class Test {

  7.     public static void main(String[] args) throws Exception {
  8.         CacheManager manager = new CacheManager("ehcache.xml");
  9.         Cache cache = manager.getCache("sampleCache1");
  10.         for (int i = 0; i < 6; i++) {
  11.             Element e = new Element("key" + i, "value" + i);
  12.             cache.put(e);
  13.         }

  14.         List<String> keys = cache.getKeys();
  15.         for (String key : keys) {
  16.             System.out.println(key + "," + cache.get(key));
  17.         }
  18.     }
  19. }

注:程序的流程也是比较明晰的,首先是获取一个CacheManager,这是使用Ehcache的入口,然后通过名字获取某个Cache,然后就可以对Cache存取Element。Cache使用类Hash的方式来管理Element。

事件处理

说明:可以为CacheManager添加事件监听,当对CacheManager增删Cache时,事件处理器将会得到通知。要配置事件处理,需要通过ehcache的配置文件来完成。

配置文件:ehcache.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache>

  3.     <defaultCache maxElementsInMemory="2" eternal="false"
  4.         timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="false"
  5.         memoryStoreEvictionPolicy="LRU" />

  6.     <cache name="sampleCache1" maxElementsInMemory="5" eternal="false"
  7.         overflowToDisk="false" timeToIdleSeconds="1" timeToLiveSeconds="1"
  8.         memoryStoreEvictionPolicy="LRU" >
  9.         <cacheEventListenerFactory class="ehcache.test.CELF"/>
  10.         
  11.      </cache>
  12. </ehcache>

注:通过来注册事件处理器的工厂类。

代码:

  1. package ehcache.test;
  2. import java.util.Properties;
  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. import net.sf.ehcache.event.CacheEventListenerFactory;
  8. public class CELF extends CacheEventListenerFactory {
  9. @Override
  10. public CacheEventListener createCacheEventListener(Properties properties) {
  11. return new CEL();
  12. }
  13. }
  14. class CEL implements CacheEventListener {
  15. public void dispose() {}
  16. public void notifyElementEvicted(Ehcache cache, Element element) {}
  17. public void notifyElementExpired(Ehcache cache, Element element) {}
  18. public void notifyElementPut(Ehcache cache, Element element)
  19. throws CacheException {
  20. System.out.println(element.getKey() + " was added.");
  21. }
  22. public void notifyElementRemoved(Ehcache cache, Element element)
  23. throws CacheException {
  24. System.out.println(element.getKey() + " was removed.");
  25. }
  26. public void notifyElementUpdated(Ehcache cache, Element element)
  27. throws CacheException {}
  28. public void notifyRemoveAll(Ehcache cache) {}
  29. @Override
  30. public Object clone() throws CloneNotSupportedException {
  31. return super.clone();
  32. }
  33. }

注:这里的代码与之前的类似,由此可见Ehcache的事件处理采用的是一种类plugin方式,也就是说,事件处理的添加是以不修改源代码为前提的。

0 0
原创粉丝点击