EHcache缓存框架详解

EhCache是一个纯Java的进程内缓存框架,具有快速、精干等特点,也是Hibernate中默认的CacheProvider。 
归纳一下它大概具有一下几个特点: 
1. 快速. 
2. 简单. 
3. 多种缓存策略 
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题 
5. 缓存数据会在虚拟机重启的过程中写入磁盘 
6. 可以通过RMI、可插入API等方式进行分布式缓存 
7. 具有缓存和缓存管理器的侦听接口 
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域 
9. 提供Hibernate的缓存实现

那么我们在开发中到底如何运用EhCache框架呢?

获取Ehcache相关jar包及帮助文档。

下载地址: http://ehcache.org/code

相关文档地址: http://ehcache.org/apidocs/

/**
  * maxElementsInMemory:缓存中允许创建的最大对象数
  * eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
  * timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,两次访问时间的最大时间间隔值, 这只能在元素不是永久驻留时有效,
  * 如果该值是 0 就意味着元素可以停顿无穷长的时间。
  * timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,
  * 如果该值是0就意味着元素可以停顿无穷长的时间。 overflowToDisk:内存不足时,是否启用磁盘缓存。
  * memoryStoreEvictionPolicy:缓存满了之后的淘汰算法
  *
  * @param args
  */
 public static void main(String[] args) {
  // 创建一个缓存管理器对象
  CacheManager cacheManager = CacheManager.create();
  // 命名缓存管理器
  cacheManager.setName("testCacheManager");
  // 创建一个指定缓存名称的缓存对象
  Cache cache = new Cache("testCache", 4, false, false, 1, 1);
  // cache.setDisabled(true);
  // 将缓存对象添加至缓存管理器
  cacheManager.addCache(cache);

  // cacheManager.shutdown();

  System.out.println("判断缓存管理器中是否存在指定的缓存对象:"
    + cacheManager.cacheExists("testCache"));
  DiskStorePathManager disStoreManager = cacheManager
    .getDiskStorePathManager();
  System.out.println("获取当前配置文件硬盘路径:"
    + disStoreManager.getFile("testCache.xml"));

  Map<String, Object> map = new HashMap<String, Object>();
  map.put("name", "tom");
  map.put("sex", "男");
  map.put("age", 1);
  // 注意:如果当前缓存对象设置了内存中最大缓存keyValue对象的话,如果超出时,则后面的覆盖前面的keyValue对象
  cache.put(new Element("cache1", map));
  cache.put(new Element("cache2", map));
  cache.put(new Element("cache3", map));

  Element element = new Element("cache4", map);
  element.setTimeToLive(1);

  cache.put(element);

  String[] cacheNames = cacheManager.getCacheNames();
  for (int i = 0; i < cacheNames.length; i++) {
   System.out.println("缓存" + i + ":" + cacheNames[i]);
  }

  // System.out.println("当前活动的缓存配置文件内容:
"
  // + cacheManager.getActiveConfigurationText());
  System.out.println("缓存管理器对象是否命名:" + cacheManager.isNamed());

  Cache testCahe = cacheManager.getCache("testCache");

  System.out.println("缓存的状态:" + testCahe.getStatus());
  System.out.println("缓存对象平均获取时间:" + testCahe.getAverageGetTime());
  System.out.println("获取缓存对象占用内存空间大小:" + testCahe.getMemoryStoreSize());
  System.out.println("获取缓存对象大小:" + testCahe.getSize());
  System.out.println("缓存是否关闭:" + testCahe.isDisabled());
  System.out.println("判断某一个缓存key是否存在在缓存中"
    + testCahe.isKeyInCache("cache3"));
  System.out.println("判断某一个缓存值是否缓存在对象中:" + testCahe.isValueInCache(map));

  // 验证缓存对象是否禁用
  if (!testCahe.isDisabled()) {
   System.out.println("判断缓存中某个对象是否过期:"
     + testCahe.isExpired(testCahe.get("cache3")));
  } else {
   System.out.println(testCahe.getName() + "缓存已关闭");
  }
  System.out.println("判断某一个key是否缓存在内存中:"
    + testCahe.isElementInMemory("cache1"));
  System.out.println("判断某一个key是否缓存在磁盘中:"
    + testCahe.isElementOnDisk("cache1"));

  System.out.println("
");

  List cacheKey = cache.getKeys();
  for (int i = 0; i < cacheKey.size(); i++) {
   Element cacheElement = testCahe.get(cacheKey.get(i));
   System.out.println("Key:" + cacheKey.get(i) + ",value:"
     + cacheElement.getObjectValue());
  }
  }
原文地址:https://www.cnblogs.com/cnsanshao/p/6098891.html