找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4278|回复: 0

进程内缓存框架 EhCache

[复制链接]
发表于 2012-2-29 15:12:23 | 显示全部楼层 |阅读模式
进程内缓存框架 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
复制代码
进一步了解
可以登陆 EhCache.org,他们的文档还蛮多的。Enjoy EhCache!
-

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-4 06:09 , Processed in 0.015954 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表