本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法

(有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 )

Cache的多种配置方法

Javabean cache的配置有三种,下面将一一介绍,具体如下::

(1)bean中注解配置的方式: @Cache(usage = CacheConcurrencyStrategy.READ_WRITE,region="")

(2)hibernate.cfg.xml中标签配置方式: <class-cache class="" usage="" region=""/>

(3)映射文件*.hb.xml中标签配置方式: <cache usage=""  region=""/>

(4)映射文件*.hb.xml中标签配置方式: 使用spring配置文件 sessionFactory 下配置方式

ehcache.xml配置说明

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

user.home – 用户主目录

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

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

<defaultCache>元素:设定缓存的默认数据过期策略,如果没有任何设置,将使用该策略。

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

    name:cache唯一标识,通常为缓存对象的类名(非严格标准),如果为实体对象的包名称.类名称(如com.db.entity.User)时,那么实体的配置中可以省去<cache usage="read-write" region="regionName"/> 属性的配置。如果不存在与类名匹配的cache名称, 则用 defaultCache。如果类中包含set

    eternal:缓存是否永久有效

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

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

    overflowToDisk(true,false):缓存对象达到最大数后,将缓存写到硬盘中

    diskPersistent:硬盘持久化。指重启JVM后,数据是否存在。默认为false。

    timeToIdleSeconds:设定允许对象处于空闲状态的最长时间,以秒为单位。

    timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。

    diskExpiryThreadIntervalSeconds:对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。

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

    memoryStoreEvictionPolicy:缓存清空策略

        1.FIFO:first in first out 先讲先出
        2.LFU: Less Frequently Used 一直以来最少被使用的
        3.LRU:Least Recently Userd 最近最少被使用

注意:

  如果要配置的实体有关联关系,1、对于一对一 、多对一 ,只要将关联对象也配置为二级缓存就可以了,否则,只缓存当前实体,对于关联对象还是每次去数据库中查询的。2、 而对于一对多则需要 在 set  或 oneToMany上加 <cache usage="read-write"/> 或   @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) ,这样也只加载关联对象的id集合,要完全缓存,则需要另外对关联的对象也配置成使用二级缓存)3、对于多对多则无法缓存关联对象,只能缓存当前对象,即使配置上面2中的所说的也无济于事(反而配置了,第二次查询时发出两条sql,不配置的话 发出一条sql)

  如果使用了延迟加载,则关联对象不会使用缓存,也就是说上面的说法都是在,没有使用延迟加载的情况下才有效。

针对查询缓存有两个专用的标签(其属性与上面用法一致):

    <cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="50"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
overflowToDisk="false" />
    <cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="false" />      

1. classpath:ehcahce.xml配置文件如下:

  Xml代码

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskSpoolBufferSizeMB="30" maxElementsOnDisk="100" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"
statistics="false" /> <cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="5" eternal="false" timeToLiveSeconds="120"
overflowToDisk="true" /> <cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000" eternal="true" overflowToDisk="true" /> <cache name="simpleCache1"
maxElementsInMemory="10000" maxElementsOnDisk="1000" eternal="false"
overflowToDisk="true" diskSpoolBufferSizeMB="20" timeToIdleSeconds="300"
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU"
transactionalMode="off" /> <cache name="simpleCache2"
maxElementsInMemory="1000" eternal="true" overflowToDisk="false"
memoryStoreEvictionPolicy="FIFO" /> <cache name="simpleCache3"
maxElementsInMemory="1000" eternal="true" overflowToDisk="false"
memoryStoreEvictionPolicy="FIFO" />
</ehcache>

2.hibernate配置文件:hibernate.cfg.xml

  Xml代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/sampledb</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.SetBigStringTryClob">true</property>
<property name="connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">10</property> <property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property> <!-- 配置二级缓存 -->
<!-- hibernate4以前的版本 配置缓存的提供类-->
<!-- <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property> -->
<!--hibernate4以后版本二级缓存的提供类-->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 二级缓存配置文件的位置 -->
<property name="hibernate.net.sf.ehcache.configurationResourceName">conf/ehcache.xml</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property> <!-- 注解配置 -->
<mapping class="michael.cache.ehcache.hibernate.EhUserInfo" />
<mapping class="michael.cache.ehcache.hibernate.EhBlogTopic" />
<mapping class="michael.cache.ehcache.hibernate.EhBlogTopic2" /> <!-- 映射文件 -->
<mapping resource="michael/cache/ehcache/hibernate/tb_EhBlogTopic3.hb.xml" /> <!-- class-cache config -->
<class-cache class="michael.cache.ehcache.hibernate.EhBlogTopic"
usage="read-write" region="simpleCache1"/> </session-factory>
</hibernate-configuration>

注:如果是Spring整合Hibernate,则可在Spring配置文件sessionFactory下配置,方法如下:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.annotation.LocalSessionFactoryBean">
<property name="entityCacheStrategies">
<props>
<prop key="michael.cache.hibernate.EhBlogTopic">read-write,simpleCache1</prop>
</props>
</property>
</bean>

3.相关javabean代码片段如下:

(1).hibernate.cfg.xml中<calss-cache>标签配置的:EhBlogTopic.java:

  Java代码

/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_EH_BLOG_TOPIC")
public class EhBlogTopic implements Serializable { /**
* serialVersionUID
*/
private static final long serialVersionUID = -570936907944909799L; private Integer id; private String userId; private String topic; private String site; //其他省略
}

(2). bean中注解的方式配置cache的:EhBlogTopic2.java

/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_EH_BLOG_TOPIC2")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE,region="simpleCache2")
public class EhBlogTopic2 implements Serializable {
//属性和EhBlogTopic一样
//其他省略
}

(3). 映射文件*.hb.xml中添加cache标签的: EhBlogTopic3.java

  Java代码

/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class EhBlogTopic3 implements Serializable {
//属性和EhBlogTopic一样
//其他省略
}

tb_EhBlogTopic3.hb.xml

  Xml代码

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="michael.cache.ehcache.hibernate"> <class name="EhBlogTopic3" table="MY_TB_EH_BLOG_TOPIC3">
<cache usage="read-write" region="simpleCache3"/>
<id name="id" type="int" unsaved-value="null">
<generator class="increment" />
</id>
<property name="userId" column="USER_ID" type="string" not-null="false" length="20" />
<property name="topic" column="TOPIC" type="string" not-null="false" length="100" />
<property name="site" column="SITE" type="string" not-null="false" length="100" /> </class>
</hibernate-mapping>

(4). 没有配置cache的bean:EhUserInfo.java

  Java代码

/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_EH_USER_INFO")
public class EhUserInfo implements Serializable { /**
* serialVersionUID
*/
private static final long serialVersionUID = 930384253681679239L; private Integer id; private String userId; private String userName; private String otherInfo; /**
* @return the id
*/
@Id
@GeneratedValue
@Column(name = "ID")
public Integer getId() {
return id;
} //其他省略。。。 }

4.测试运行代码如下:

  Java代码

/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestEhcacheHibernate { /**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
testMulitConfigMethod();
} /**
* 测试多种配置缓存的方法
*/
public static void testMulitConfigMethod() {
SessionFactory sessionFactory = null;
try {
System.out.println("ehcache - hibernate Test ...");
Configuration config = new AnnotationConfiguration()
.configure("michael/cache/ehcache/hibernate/hibernate.cfg.xml");
System.out.println("hibernate config successful :" + config);
sessionFactory = config.buildSessionFactory();
Transaction ta = null;
try {
Session session = sessionFactory.getCurrentSession();
ta = session.beginTransaction();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
String[] cacheNames = CacheManager.getInstance().getCacheNames();
System.out.println("缓存的key cacheNames length := "
+ cacheNames.length + " 具体详细列表如下:");
for (String name : cacheNames) {
System.out.println("name := " + name);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ehcache - hibernate Test end.");
}
}

运行结果如下:

ehcache - hibernate Test ...

hibernate config successful :org.hibernate.cfg.AnnotationConfiguration@193c0cf

2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache

警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.

2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache

警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic2]; using defaults.

2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache

警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic3]; using defaults.

2011-12-15 11:32:37 net.sf.ehcache.util.UpdateChecker doCheck

信息: New update(s) found: 2.4.6 [http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.4]. Please check http://ehcache.org for the latest version.

缓存的key cacheNames length := 7 具体详细列表如下:

name := sampleCache2

name := michael.cache.ehcache.hibernate.EhBlogTopic2

name := org.hibernate.cache.UpdateTimestampsCache

name := sampleCache1

name := michael.cache.ehcache.hibernate.EhBlogTopic

name := org.hibernate.cache.StandardQueryCache

name := michael.cache.ehcache.hibernate.EhBlogTopic3

ehcache - hibernate Test end.

从运行结果可见:三种方式的缓存配置都已经成功。

原文出自:http://jyao.iteye.com/blog/1315726

Hibernate4+EhCache配置二级缓存的更多相关文章

  1. mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存

    增删改对二级缓存的影响 1.增删改也会清空二级缓存 2.对于二级缓存的清空实质上是对value清空为null,key依然存在,并非将Entry<k,v>删除 3.从DB中进行select查 ...

  2. Hibernate+EhCache配置二级缓存

    步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配 ...

  3. ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存

    ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存 hibernate  : Hibernate是一个持久层框架,经常访问物理数据库 ...

  4. (转)为Spring集成的Hibernate配置二级缓存

    http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...

  5. SpringBoot30 整合Mybatis-Plus、整合Redis、利用Ehcache实现二级缓存、利用SpringCache和Redis作为缓存

    1 环境说明 JDK: 1.8 MAVEN: 3. SpringBoot: 2.0.4 2 SpringBoot集成Mybatis-Plus 2.1 创建SpringBoot 利用IDEA创建Spri ...

  6. Hibernate4.1.4配置二级缓存EHCache步骤

    1.当然首先引入EHCache相关的jar包 这些包不需要另外下载,在Hibernate官方网站下载Hibernate4.1.7的压缩包(如:hibernate-release-4.1.7.Final ...

  7. SSH整合配置二级缓存

    一.了解 Hibernate的session提供了一级缓存,每个session,对同一个id进行两次load,不会发送两条sql给数据库,但session关闭时,一级缓存失效. 二级缓存是Sessio ...

  8. Hibernate 集成 Ehcache 开启二级缓存

    一.将 Ehcache.xml 放到 classpath 下 <?xml version="1.0" encoding="UTF-8"?> < ...

  9. Spring Boot2.0+Redis+Ehcache实现二级缓存

    EHCache 本地缓存 Redis 分布式缓存(可以共享) 一级 Redis 二级Ehcache    当redis挂了 有备胎 反之: 先走本地,本地没有再走网络  尽量少走Redis  效率会高 ...

随机推荐

  1. SpringMVC基础-@RequestMapping

    @RequestMapping @RequestMapping是一个用来处理请求地址映射的注解 可用于类或方法上.   类定义处:提供初步的请求映射信息.相当于当前 WEB 应用的根目录   方法处: ...

  2. (转载)2016 CCF大数据与计算智能大赛 开源资料整理

    本文转载自:http://blog.sina.com.cn/s/blog_5399b8660102wxks.html 2016 CCF 大数据与计算智能大赛已经落下帷幕,11个赛题由众多大神包揽奖项, ...

  3. SoapUI进行接口测试,怎么应对接口地址总是变化!

    如果是没有代码能力的小白,要利用工具进行接口测试的时候,经常会遇到接口地址或者接口参数变化的问题,然后不得不在他们改了接口之后,就手动去改所有的请求链接地址和接口参数!1-5个请求,我们手动改还应付的 ...

  4. hdu 5040 Instrusive

    Instrusive Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tota ...

  5. JavaScript基本语法2

    javaScript中要显示> .<必须使用&gt .&lt,如果是不显示但是要作为条件可以直接使用>.<. 接下来是JavaScirpt函数: 要使用func ...

  6. NOIP[2015] Day2题解

    问题 A: 跳石头 时间限制: 1 Sec  内存限制: 128 MB 题目描述 一年一度的"跳石头"比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石. ...

  7. HTML <area><map>标签及在实际开发中的应用

    之前,我一直以为HTML <area>是一个鸡肋HTML,估计到了HTML5时代会被废弃的命.但是,最近一查资料,乖乖了个咚,不仅没被废弃,反而发展了,新增了一些标签属性,例如rel,me ...

  8. 【Java学习笔记之三十三】详解Java中try,catch,finally的用法及分析

    这一篇我们将会介绍java中try,catch,finally的用法 以下先给出try,catch用法: try { //需要被检测的异常代码 } catch(Exception e) { //异常处 ...

  9. Python虚拟环境virtualenv

    用意 virtualenv可以搭建虚拟且独立的python运行环境, 使得单个项目的运行环境与其它项目独立起来. 安装 virtualenv本质上是个python包, 使用pip安装 pip inst ...

  10. 九度OJ 1013 开门人和关门人

    #include <iostream> #include <string.h> #include <sstream> #include <math.h> ...