简介
Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。—百度百科
使用
1.java项目中用ehcache 添加ehcache.jar在项目lib 文件夹下,并在java build path 里 add jars 加入lib下的jar引入依赖jar commons-logging.jar在 meta-inf 文件夹下 manifest.mf 中 runtime classpath 中add 导入 ehcache.jar
2.创建工具类EhcacheUtil (其中 path 为ehcache.xml配置文件的存放路径),具体代码如下:
package webc; import java.net.URL; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; public class EhcacheUtil { private static final String path = "/ehcache/ehcache.xml"; private URL url; private CacheManager manager; private static EhcacheUtil ehCache; public EhcacheUtil() { url = getClass().getResource("/ehcache/ehcache.xml"); manager = CacheManager.create(url); } public static EhcacheUtil getInstance() { if (ehCache== null) { ehCache= new EhcacheUtil(); } return ehCache; } public void put(String cacheName, String key, Object value) { Cache cache = manager.getCache(cacheName); Element element = new Element(key, value); cache.put(element); } public Object get(String cacheName, String key) { Cache cache = manager.getCache(cacheName); Element element = cache.get(key); return element == null ? null : element.getObjectValue(); } public Cache get(String cacheName) { return manager.getCache(cacheName); } public void remove(String cacheName, String key) { Cache cache = manager.getCache(cacheName); cache.remove(key); } }
4.创建ehcache.xml,在这里主要进行缓存的配置,具体说明如下
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <!-- 指定缓存目录 --> <diskStore path="java.io.tmpdir"/><!-- 达到内存上限后缓存文件保存位置 --> <!-- 默认缓存配置 name:缓存名称。 maxElementsInMemory:缓存最大个数。 eternal:对象是否永久有效,一但设置了,timeout将不起作用。 timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 maxElementsOnDisk:硬盘最大缓存个数。 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush:内存数量最大时是否清除。 --> <!--timeToIdleSeconds 当缓存闲置n秒后销毁 --> <!--timeToLiveSeconds 当缓存存活n秒后销毁 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskPersistent="false" /> <!-- name缓存名称 --> <cache name="mytest" memoryStoreEvictionPolicy="LRU" maxElementsInMemory="1" eternal="false" timeToIdleSeconds="7200" timeToLiveSeconds="7200" overflowToDisk="true" /> </ehcache>
5.主程序中的使用(代码中“mytest”为ehcache.xml中定义的缓存名称)下面上测试代码
public static void main(String[] args) { // TODO Auto-generated method stub String ClassName = Thread.currentThread().getStackTrace()[2].getClassName(); // 类名 String MethodName= Thread.currentThread().getStackTrace()[2].getMethodName(); //获取方法名 String key=ClassName+MethodName; EhcacheUtil eh=new EhcacheUtil(); if(eh.get("mytest", key)!=""&&eh.get("mytest", key)!=null){ System.out.println("1"+eh.get("mytest", key)); }else{ eh.put("mytest", key, "value"); System.out.println("2"+eh.get("mytest", key)); } }
6.另一种情况,也可以这样配置:
URL url = getClass().getResource("/ehcache/ehcache.xml"); CacheManager singletonManager = CacheManager.create(url); //建立一个缓存实例 Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2); //在内存管理器中添加缓存实例 singletonManager.addCache(memoryOnlyCache); Cache cache = singletonManager.getCache("testCache"); //使用缓存 Element element = new Element("key1", "value1"); cache.put(element); cache.put(new Element("key1", "value2")); element = cache.get("key1"); Serializable value = element.getValue(); System.out.println(value); int elementsInMemory = cache.getSize(); System.out.println(elementsInMemory); long elementsInMemory2 = cache.getMemoryStoreSize(); System.out.println(elementsInMemory2); Object obj = element.getObjectValue(); cache.remove("key1"); System.out.println(obj); singletonManager.shutdown(); // manager.shutdown(); System.out.println(2);
总结
文章转载自中学生数学与编程,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




