EHcache缓存框架详解

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
   * 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("当前活动的缓存配置文件内容:\n"
   // + 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( "\n" );
  
   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());
   }
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值