ehcache入门

一、简介

  ehcache是一个开源的,纯java进程内的缓存框架。它具有快速,简单,具有多种缓存策略等特点。 Hibernate中默认就是用了ehcache。在我们的应用中使用ehcache可以快速地提高应用的性能。ehcache主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api。

二、实例

  ehcache下载地址为:http://www.ehcache.org/downloads/catalog 。一般我们使用ehcahe会使用配置文件进行缓存配置。先创建ehcache.xml文件,在该文件定义我们使用的cache名称,大小,调度算法等相关缓存配置。并将该文件存放在系统路径的src目录下。ehcache.xml文件内容如下:  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
 4     updateCheck="false">
 5 
 6     <!-- 默认缓存配置 ,缓存名称为 default -->
 7     <defaultCache maxElementsInMemory="50" eternal="false"
 8         overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
 9     <!-- 自定义缓存,名称为lt.ehcache -->
10     <cache name="lt.ecache" maxElementsInMemory="50" eternal="false"
11         overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
12 </ehcache>  

  name缓存的名称,必须唯一;overflowToDisk表示内存放满之后缓是否保存到硬盘上;memoryStoreEvictionPolicy 表示页面调度算法;eternal表示缓存是否过期;timeToIdleSeconds表示对象在多长时间没有被访问就会失效; timeToLiveSeconds表示对象存活时间,指对象从创建到失效所需要的时间。 maxElementsInMemory表示缓存元素的个数;maxElementsOnDisk表示在磁盘上缓存的element的最大数目,默认值为0,表示不限制。

  调用代码如下:

 1 package com.ehcache.simple;
 2 
 3 import java.util.List;
 4 
 5 import net.sf.ehcache.Cache;
 6 import net.sf.ehcache.CacheManager;
 7 import net.sf.ehcache.Element;
 8 import net.sf.ehcache.config.CacheConfiguration;
 9 import net.sf.ehcache.config.Configuration;
10 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
11 
12 public class SimpleTest {
13     public static void main(String[] args) {
14         // InputStream in = SimpleTest.class.getClassLoader().getResourceAsStream("ehcache.xml");
15         // URL url = SimpleTest.class.getClassLoader().getResource("ehcache.xml");
16         // URL url2 = SimpleTest.class.getResource("ehcache.xml");  
17         String path = System.getProperty("ehcache.xml");  
18         CacheManager manager = CacheManager.create(path); 
19             
20         //创建Cache对象
21         Cache cache = manager.getCache("lt.ecache");
22 
23         //cache缓存名称
24         System.out.println("cache name: " + cache.getName());
25         
26         //将对象放入缓存    
27         Element element = new Element("hello", "world");
28         Element element2 = new Element("aaa", "111");
29         Element element3 = new Element("bbb", "222");
30         Element element4 = new Element("bbb", "222");
31         cache.put(element);
32         cache.put(element2);
33         cache.put(element3);
34         cache.put(element4);//key相同时会被覆盖
35         
36         //cache缓存对象个数
37         System.out.println("size: " + cache.getSize());
38         
39         // 从cache中取回元素
40         System.out.println("hello: " + cache.get("hello").getValue());
41         
42         List<String> keys = cache.getKeys();//所有缓存对象的key
43         
44         // 遍历所有缓存对象
45         for(String key : keys ){
46             System.out.println(key + " : " + cache.get(key));
47         }
48         
49         // 从Cache中移除一个元素
50         System.out.println(cache.remove("hello")); 
51         System.out.println(cache.remove("hello2")); 
52         
53         //移除所有缓存对象
54         cache.removeAll();
55         
56         System.out.println("size: " + cache.getSize());
57         
58         manager.shutdown();
59     }
60 
61 }

  ehcache.xml在系统下,通过加载ehcache.xml创建缓存对象。运行结果如下:

  除了使用配置文件创建Cache对象之外,我们也可以通过代码的方式创建。相关代码如下:

 1         CacheConfiguration cacheConfig = new CacheConfiguration("lt.ecache", 50)
 2                 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) // 设置调度算法
 3                 .overflowToDisk(true) // 设置是否缓存到硬盘
 4                 .eternal(false) // 设置是否过期
 5                 .timeToLiveSeconds(60) // 对象存活时间
 6                 .timeToIdleSeconds(30) // 调度设置最大不活动时间
 7                 .diskPersistent(false) // 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
 8                 .diskExpiryThreadIntervalSeconds(0);// 设置对象检测线程运行时间间隔
 9         Configuration config = new Configuration();
10         config.addCache(cacheConfig);
11         CacheManager manager = CacheManager.create(config);
12 
13         // 创建Cache对象
14         Cache cache = manager.getCache("lt.ecache");

posted on 2014-11-20 21:35  烟火_  阅读(1860)  评论(0编辑  收藏  举报

导航