本文主要讲一讲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. IT科技企业逻辑思维面试题

    逻辑思维面试题 一.假设有一个池塘,里面有无穷多的水.现有2个空水壶,容积分别为5升和6升.问题是如何只用这2个水壶从池塘里取得3升的水.[请描述操作过程] 答:(1)先用容积为6升的水壶装满水: ( ...

  2. STL中map的用法

    map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...

  3. 迈向angularjs2系列(4):脏值检测机制

    目录 一: 概念简介 脏值检测,简单的说就是在MVC的构架中,视图会通过模型的change事件来更新自己. 脏值检测的核心代码是观察者模式的实现,其机制会执行digest循环,在特定UI组件的上下文执 ...

  4. Servlet和Filter的url匹配以及url-pattern详解 及 filter 循环问题的解决

    Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单,老少皆宜.估计大多数朋友都是直接配置用,也没有关心过具体的细节,今天遇到一个问题,上网查了servlet的规范才发现,ser ...

  5. https 协议下服务器根据网络地址下载上传文件问题

    https 协议下服务器根据网络地址下载上传文件遇到(PKIX:unable to find valid certification path to requested target 的问题) 使用h ...

  6. TCP/IP协议栈模型

    OSI七层模型介绍: 下面4层(物理层.数据链路层.网络层和传输层)主要提供数据传输和交换功能,即以节点到节点之间的通信为主:第4层作为上下两部分的桥梁,是整个网络体系结构中最关键的部分:而上3层(会 ...

  7. js实现换肤效果

    一,js换肤的基本原理 基本原理很简单,就是使用 JS 切换对应的 CSS 样式表文件.例如导航网站 Hao123 的右上方就有网页换肤功能.除了切换 CSS 样式表文件之外,通常的网页换肤还需要通过 ...

  8. JavaScript中你所不知道的Object(一)

    Object实在是JavaScript中很基础的东西了,在工作中,它只有那么贫瘠的几个用法,让人感觉不过尔尔,但是我们真的了解它吗? 1. 当我们习惯用 var a = { name: 'tarol' ...

  9. 波涛1202wm8833 lihomme/历织造 2013秋装全新男装夹克 整身年龄外套潮流立领男士休闲外套薄_9才号

    波涛1202wm8833 lihomme/历织造 2013秋装全新男装夹克 整身年龄外套潮流立领男士休闲外套薄_9才号 波涛1202wm8833lihomme/历织造2013秋装全新男装夹克整身年龄外 ...

  10. Python 进程与线程小随笔

    Process 涉及模块:multiprocessing Process p = Process() p.start() p.join() from multiprocessing import Pr ...