免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2012 | 回复: 1
打印 上一主题 下一主题

进程内缓存框架EhCache [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-05 22:01 |只看该作者 |倒序浏览

进程内缓存框架EhCache








进程内缓存框架 EhCache
• Author: Poechant
• Blog: blog.CSDN.net/Poechant
• Email: zhongchao.ustc#gmail.com (#->@)
• Date: February 27th, 2012

1. What the hell is EhCache?



EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。EhCache 具有以下特点(摘自开源中国社区)。
• 快速
• 简单
• 多种缓存策略
• 缓存数据有两级:内存和磁盘,因此无需担心容量问题
• 缓存数据会在虚拟机重启的过程中写入磁盘
• 可以通过 RMI、可插入 API 等方式进行分布式缓存
• 具有缓存和缓存管理器的侦听接口
• 支持多缓存管理器实例,以及一个实例的多个缓存区域
• 提供 Hibernate 的缓存实现

2. 常用 API

构造函数

方法原型
  1. public Cache(String name,
  2.          int maxElementsInMemory,
  3.          boolean overflowToDisk,
  4.          boolean eternal,
  5.          long timeToLiveSeconds,
  6.          long timeToIdleSeconds)
复制代码
该构造函数是 EhCache 1.0 版本中的出现的,后续开发到 2.0 之后,出现了带有 CacheConfiguration 参数的构造函数,用法更强大。比如如下这个:
  1. public Cache(CacheConfiguration cacheConfiguration,
  2.          RegisteredEventListeners registeredEventListeners,
  3.          BootstrapCacheLoader bootstrapCacheLoader)
复制代码
用法可参见 EhCache 的 API Docs。

参数含义
• name - 缓存的名称,default`保留为默认缓存名称;
• maxElementsInMemory - 内存中的最大同时缓存元素个数;
• overflowToDisk - 是否持久化(使用磁盘);
• eternal - 对象是否永久有效(永不过期);
• timeToLiveSeconds - 对象从其创建开始计算的生存时间(秒);
• timeToIdleSeconds - 对象从其最后一次被访问开始计算的生存时间(秒)。

一般来说,只有 name、maxElementsInMemory 和 timeToLiveSeconds 显式设置,其他的参数常根据其不同的类型而设置为 false、0 等。

put 方法

方法原型

  1. public final void put(Element element)
  2.            throws IllegalArgumentException,
  3.                   IllegalStateException,
  4.                   CacheException
复制代码
参数含义
• element:所要存储的元素,可参见其定义 Element API Docs。它是 EhCache 中缓存的基本单元。

get 方法

方法原型

  1. public final Element get(Object key)
  2.               throws IllegalStateException,
  3.                      CacheException
复制代码
参数含义
• key:可以为任意类型的 key,比较灵活。

remove 方法

方法原型

  1. public final boolean remove(Object key)
  2.                  throws IllegalStateException
复制代码
参数含义
• key:指定的 key。

3. 示例

源码
  1. package com.sinosuperman.ehcache;

  2. import net.sf.ehcache.Cache;
  3. import net.sf.ehcache.Element;

  4. public class Test {

  5.     public static void main(String[] args) {

  6.         String name = "Namespace";
  7.         int capacity = 500;
  8.         int refreshPeriod = 5000;

  9.         // Initialize EhCache
  10.         Cache cache = new Cache(name, capacity, false, false, refreshPeriod, 0);
  11.         cache.initialise();
  12.         System.out.println(
  13.             "Initialize EhCache: " +
  14.             "   name:       " + name +
  15.             "   capacity:   " + capacity +
  16.             "   expire:     " + refreshPeriod
  17.         );

  18.         // Set data into EhCache
  19.         String key1 = "Key";
  20.         String value1 = "Value";
  21.         Element element1 = new Element(key1, value1);
  22.         cache.put(element1);
  23.         System.out.println("Set (" + key1 + ", " + value1 + ") into EhCache.");

  24.         // Get data from EhCache
  25.         Element element2 = cache.get(key1);
  26.         String key2 = (String) element2.getObjectKey();
  27.         String value2 = (String) element2.getObjectValue();
  28.         System.out.println("Get (" + key2 + ", " + value2 + ") from EhCache.");

  29.         // Remove data from EhCache
  30.         if (cache.remove(key2)) {
  31.             System.out.println("Remove data with key = " + key2 + " successfully.");
  32.         }

  33.         // Get EhCache size
  34.         System.out.println("EhCache size is " + cache.getSize());
  35.     }
  36. }
复制代码
结果

  1. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  2. SLF4J: Defaulting to no-operation (NOP) logger implementation
  3. SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  4. Initialize EhCache:     name:       Namespace   capacity:   500 expire:     5000
  5. Set (Key, Value) into EhCache.
  6. Get (Key, Value) from EhCache.
  7. Remove data with key = Key successfully.
  8. EhCache size is 0
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-05 22:02 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP