源文:

http://blog.csdn.net/l271640625/article/details/20528573

一、简介

非常简单,而且易用。

    ehcache 是一个非常轻量级的缓存实现,而且从1.2 之后就支持了集群,而且是hibernate 默认的缓存provider。ehcache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

ehcache可以直接使用。也可以和Hibernate对象/关系框架结合使用。还可以做Servlet缓存。

Cache 存储方式 :内存或磁盘。

官方网站:http://ehcache.sourceforge.net/

主要特征:

1. 快速.

2. 简单.

3. 多种缓存策略

4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题

5. 缓存数据会在虚拟机重启的过程中写入磁盘

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

7. 具有缓存和缓存管理器的侦听接口

8. 支持多缓存管理器实例,以及一个实例的多个缓存区域

9. 提供Hibernate的缓存实现

10. 等等

二、快速上手

1、  项目类库中添加ehcache.jar;

2、  在类路径下编写ehcache.xml配置文件。

三、配置文件参数详解

ehcache.xml是ehcache的配置文件,并且存放在应用的classpath中。下面是对该XML文件中的一些元素及其属性的相关说明: 

<diskStore>元素:指定一个文件目录,当EHCache把数据写到硬盘上时,将把数据写到这个文件目录下。 下面的参数这样解释:   

         user.home – 用户主目录   

         user.dir      – 用户当前工作目录   

         java.io.tmpdir – 默认临时文件路径

<defaultCache>元素:设定缓存的默认数据过期策略。 

<cache>元素:设定具体的命名缓存的数据过期策略。

<cache>元素的属性 

name:缓存名称。通常为缓存对象的类名(非严格标准)。 

maxElementsInMemory:设置基于内存的缓存可存放对象的最大数目。 

maxElementsOnDisk:设置基于硬盘的缓存可存放对象的最大数目。  

eternal:如果为true,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false; 

timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态。 

timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义。 

overflowToDisk:如果为true,表示当基于内存的缓存中的对象数目达到了maxElementsInMemory界限后,会把益出的对象写到基于硬盘的缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。

memoryStoreEvictionPolicy:缓存对象清除策略。有三种:

1 FIFO ,first in first out ,这个是大家最熟的,先进先出,不多讲了

2 LFU , Less Frequently Used ,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。

2 LRU ,Least Recently Used ,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。

四、单独使用EHCache

1.创建CacheManager (net.sf.ehcache.CacheManager)

(1)使用默认配置文件创建

1 CacheManager manager = CacheManager.create();

(2)使用指定配置文件创建

1 CacheManager manager = CacheManager.create("src/config/ehcache.xml");

(3)从classpath找寻配置文件并创建

1 URL url = getClass().getResource("/anothername.xml");
2 CacheManager manager = CacheManager.create(url);

(4)通过输入流创建

1 InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
2 try { 
3     manager = CacheManager.create(fis);
4  } finally { 
5     fis.close(); 
6 }

2.创建Caches (net.sf.ehcache.Cache)

(1)取得配置文件中预先 定义的sampleCache1设置,生成一个Cache

1 Cache cache = manager.getCache("sampleCache1");

(2)设置一个名为test 的新cache,test属性为默认

1 CacheManager manager = CacheManager.create();
2 manager.addCache("test");

(3)设置一个名为test 的新cache,并定义其属性

1 CacheManager manager = CacheManager.create();
2 Cache cache = new Cache("test", 1, true, false, 5, 2);
3 manager.addCache(cache);

(4)删除cache

1 CacheManager singletonManager = CacheManager.create();
2 singletonManager.removeCache("sampleCache1");

3.使用Caches

(1)往cache中加入元素

1 Element element = new Element("key1", "value1");
2 cache.put(new Element(element);

(2)从cache中取得元素

1 Element element = cache.get("key1");

(3)从cache中删除元素

1 Cache cache = manager.getCache("sampleCache1");
2 Element element = new Element("key1", "value1");
3 cache.remove("key1");

4.卸载CacheManager ,关闭Cache

1  manager.shutdown();

下附代码。

五、在 Hibernate 中运用EHCache

1、hibernate.cfg.xml中需设置如下:

3系列版本加入

1 <property name=” hibernate.cache.provider_class”>
2     org.hibernate.cache.EhCacheProvider
3 </property>

EhCacheProvider类位于hibernate3.jar

2.1版本加入

net.sf.ehcache.hibernate.Provider

2.1以下版本加入

net.sf.hibernate.cache.EhCache

 2、在Hibernate3.x中的etc目录下有ehcache.xml的示范文件,将其复制应用程序的src目录下(编译时会把ehcache.xml复制到WEB-INF/classess目录下),对其中的相关值进行更改以和自己的程序相适合。

 3、持久化类的映射文件进行配置

1 <cache usage="read-write"/>

在<set>标记中设置了<cache usage="read-write"/>,但Hibernate仅把和Group相关的Student的主键id加入到缓存中,如果希望把整个Student的散装属性都加入到二级缓存中,还需要在Student.hbm.xml文件的<class>标记中加入<cache>子标记,如下所示:

1 <cache usage="read-write" /> <!--cache标记需跟在class标记后-->

注:SSH中hibernate配置的cache信息

1 <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>

配置实例:

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
       
<ehcache>
        
   
  
    <diskStore path="java.io.tmpdir"/>--------------这一行很重要不然会报错
     <!--
    <cacheManagerEventListenerFactory class="" properties=""/>
   
    <cacheManagerPeerProviderFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
            properties="peerDiscovery=automatic,
                        multicastGroupAddress=230.0.0.1,
                        multicastGroupPort=4446, timeToLive=1"
            propertySeparator=","
            />

<cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/>
    -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="36000"
            timeToLiveSeconds="86400"
            overflowToDisk="false"
            />
            <!--
            diskSpoolBufferSizeMB="30"
            maxElementsOnDisk="100000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            -->
    <cache name="reCache"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="36000"
            timeToLiveSeconds="86400"
            overflowToDisk="false"
            />

<!--<cache name="sampleDistributedCache1"
           maxElementsInMemory="10"
           eternal="false"
           timeToIdleSeconds="100"
           timeToLiveSeconds="100"
           overflowToDisk="false">
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
        <bootstrapCacheLoaderFactory
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
    </cache>-->

</ehcache>

缓存之EHCache(一)的更多相关文章

  1. 缓存插件 EHCache 对象缓存(Spring)

    对象缓存就是将查询的数据,添加到缓存中,下次再次查询的时候直接从缓存中获取,而不去数据库中查询. 对象缓存一般是针对方法.类而来的,结合Spring的Aop对象.方法缓存就很简单.这里需要用到切面编程 ...

  2. 缓存初解(三)---Spring3.0基于注解的缓存配置+Ehcache和OScache

    本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见: Spring基于注解的缓存配置--web应用实例 一.简介 在spring的modules包中 ...

  3. spring+springMVC+JPA配置详解(使用缓存框架ehcache)

    SpringMVC是越来越火,自己也弄一个Spring+SpringMVC+JPA的简单框架. 1.搭建环境. 1)下载Spring3.1.2的发布包:Hibernate4.1.7的发布包(没有使用h ...

  4. Java的进程内缓存框架:EhCache (转)

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache缓存的特点: 1. 快速. 2. 简单. 3. 多种缓存 ...

  5. Spring Boot 揭秘与实战(二) 数据缓存篇 - EhCache

    文章目录 1. EhCache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 EhCache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门 ...

  6. Java的进程内缓存框架:EhCache

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.   Ehcache缓存的特点: 1. 快速. 2. 简单. 3. 多种 ...

  7. 缓存框架EhCache的简单使用

    缓存框架EhCache的简单使用: 1.Spring和EhCache框架整合 1.1导入jar包 <dependencies> <dependency> <groupId ...

  8. SpringMVC集成缓存框架Ehcache

    在互联网应用中,应用并发比传统企业及应用会高出很多.解决并发的根本在于系统的响应时间与单位时间的吞吐量.思路可分为:一减少系统的不必要开支(如缓存),二是提高系统单位时间内的运算效率(如集群). 在硬 ...

  9. 缓存插件 EHCache 页面缓存CachingFilter

    Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.get ...

  10. 缓存插件 EHCache

    EHCache是来自sourceforge(http://ehcache.sourceforge.net/)的开源项目,也是纯Java实现的简单.快速的Cache组件. 下载jar包 Ehcache ...

随机推荐

  1. Linux下JDK+Eclipse安装

    Ubuntu版本14.04 JDK8_144 eclipse最新下载 注:原本安装JDK7配置好环境后报错,原来是最新eclipse的一个功能只有JDK8支持,若想使用JDK7需要注释某条代码 JDK ...

  2. poj 2186 "Popular Cows"(强连通分量入门题)

    传送门 参考资料: [1]:挑战程序设计竞赛 题意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系具有传递性,所以如果牛A认为牛 ...

  3. (string find) 亲和串 hdu2203

    亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. 《Linux就该这么学》第二期视频

    Linux就该这么学--第二期学习笔记... ------------- 你的未来取决于你现在点点滴滴的努力 需要用到的一些工具: Vm11激活码 ---------- root在Linux系统中相当 ...

  5. go 数组与切片

    数组概念 1.数组:是同一种数据类型的固定长度的序列. 2.数组定义:var a [len]int,比如:var a[5]int,一旦定义,长度不能变 3.长度是数组类型的一部分,因此,var a[5 ...

  6. 1、JDBC-Connection

    新建Maven工程 pom.xml <dependencies> <dependency> <groupId>mysql</groupId> <a ...

  7. maven中经常使用的插件

    tomcat插件:非常实用,特点就是不用配置tomcat,可以任意修改端口号. <plugin> <groupId>org.apache.tomcat.maven</gr ...

  8. JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮【转】

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  9. POJ - 1584 A Round Peg in a Ground Hole(判断凸多边形,点到线段距离,点在多边形内)

    http://poj.org/problem?id=1584 题意 按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包. 再给定一个圆形(圆心坐标和半径),判断这个圆是否完全 ...

  10. ThinkPHP 3.2 vendor()方法的深入研究及Phpqrcode的正确扩展

    ThinkPHP vendor 方法导入第三方类库 第三方类库 第三方类库指除了 ThinkPHP 框架.应用项目类库之外的其他类库,一般由第三方系统或产品提供,如 Smarty.Zend 等系统的类 ...