EHCache的使用

来源:互联网 发布:网络直播广告植入 编辑:程序博客网 时间:2024/04/28 07:57

写EHCache之前我在百度上搜了一下缓存的好处,众多赞美之词赋予了它,所以今天来看下EHCache的使用

在网上听别人说了这样的一句话。我感觉很有意思,当你的程序80%的时间在访问20%的资源时,哥们!你需要有个缓存了。

EHCache是一个纯java的进程内缓存框架,具有快速、精干的特点

1、快速

在EHCache发行的一段时间内,经过几年的努力和不计其数的性能测试,EHCache终被设计于large,high concurrency systems

2、简单

搭建EHCache框架只需求占用您的几分钟时间,而且只用到了cxf jar包(不过我多下了几个,网上说是用cxf的jar包,但是我还是用的log4j比较靠谱,这个上网下,到处都是)

3、缓存有两级:内存和硬盘,因此不用担心缓存问题

4、缓存数据会在jvm重启的过程中写入磁盘

5、可以通过RMI、可插入API等方式进行分布式缓存

首先来看下ehcache的配置文件

<pre name="code" class="html"><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="ehcache.xsd">  <diskStore path="java.io.tmpdir"/>  <defaultCache    maxElementsInMemory="10000"    maxElementsOnDisk="0"    eternal="true"    overflowToDisk="true"    diskPersistent="false"    timeToIdleSeconds="0"    timeToLiveSeconds="0"    diskSpoolBufferSizeMB="50"    diskExpiryThreadIntervalSeconds="120"    memoryStoreEvictionPolicy="LFU"    />  <cache name="demoCache"    maxElementsInMemory="100"    maxElementsOnDisk="0"    eternal="false"    overflowToDisk="false"    diskPersistent="false"    timeToIdleSeconds="119"    timeToLiveSeconds="119"    diskSpoolBufferSizeMB="50"    diskExpiryThreadIntervalSeconds="120"    memoryStoreEvictionPolicy="FIFO"    /></ehcache>

name:Cache的名称,必须是唯一的(ehcache会把这个cache放到hashMap里)

maxElmenetsInMemory:内存中保持的对象数量

maxElementsOnDisk:DiskStore中保持的对象数量,默认为0,表示不限制

eternal:是否是永恒的数据,如果是,则它的超过设置会被忽略

overflowToDisk:如果内存中的数量超过maxElementsInMemory限制,是否要缓存到磁盘上

diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效,默认为false

timeToIdleSeconds:对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false有效,默认为0表示一直可以访问

timeToLiveSeconds:对象存活时间,指对象从创建到失效所需要的时间,只对eternal 为false有效,默认为0表示一直可以访问

diskSpoolBufferSizeMB:DiskStore使用的磁盘大小,默认值为30m。每个cache使用各自的diskstore

memoryStoreEvictionPolicy:如果内存中数据超过内存限制,向磁盘缓存的策略。默认值为LRU,可选 FIFO,LFU

package com.huawei.dao;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;public class TestEhCache {public static void ehcache(){long startTime = System.currentTimeMillis();//CacheManager类它负责读取配置文件,默认读取classpath下的ehcache.xml,根据配置文件创建//关管理Cache对象,如果不读取默认的配置文件://CacheManager manager = CacheManager.create("src/xxx.xml");CacheManager manager = CacheManager.create();//通过manager可以生成指定名称的cache对象Cache cid = manager.getCache("demoCache");for(int i=0;i<100;i++){Person person = new Person();person.setId(i+"");person.setName("person"+i);cid.put(new Element(i,person));}long endTime = System.currentTimeMillis();System.out.println("程序运行了:"+(endTime-startTime)+",程序已经启动");}public void testEhCache(){CacheManager manager = CacheManager.create();Cache cid = manager.getCache("demoCache");Element element = cid.get(1);Person person = (Person) element.getValue();System.out.println(person.getId()+"---"+person.getName());}public void remove(){CacheManager manager = CacheManager.create();manager.removeCache("demoCache");}public static void main(String[] args){TestEhCache  test = new TestEhCache();test.ehcache();test.testEhCache();}}

这里person是我自己建的一个非常简单的实体类,一个id,一个name全是string类型的。在这里就补上代码了

初看Ehcache还是比较简单的。用spring整合的话比较麻烦一点。下一篇会写spring和ehcache的整合会涉及到aop其实aop我工作了一年多了,,也很少碰到,所以我也是学习。哈哈。。。。




0 0
原创粉丝点击