Hibernate4+EhCache配置二级缓存
本文主要讲一讲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配置二级缓存的更多相关文章
- mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存
增删改对二级缓存的影响 1.增删改也会清空二级缓存 2.对于二级缓存的清空实质上是对value清空为null,key依然存在,并非将Entry<k,v>删除 3.从DB中进行select查 ...
- Hibernate+EhCache配置二级缓存
步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配 ...
- ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存
ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存 hibernate : Hibernate是一个持久层框架,经常访问物理数据库 ...
- (转)为Spring集成的Hibernate配置二级缓存
http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...
- 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 ...
- Hibernate4.1.4配置二级缓存EHCache步骤
1.当然首先引入EHCache相关的jar包 这些包不需要另外下载,在Hibernate官方网站下载Hibernate4.1.7的压缩包(如:hibernate-release-4.1.7.Final ...
- SSH整合配置二级缓存
一.了解 Hibernate的session提供了一级缓存,每个session,对同一个id进行两次load,不会发送两条sql给数据库,但session关闭时,一级缓存失效. 二级缓存是Sessio ...
- Hibernate 集成 Ehcache 开启二级缓存
一.将 Ehcache.xml 放到 classpath 下 <?xml version="1.0" encoding="UTF-8"?> < ...
- Spring Boot2.0+Redis+Ehcache实现二级缓存
EHCache 本地缓存 Redis 分布式缓存(可以共享) 一级 Redis 二级Ehcache 当redis挂了 有备胎 反之: 先走本地,本地没有再走网络 尽量少走Redis 效率会高 ...
随机推荐
- 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——收流篇:(四)example代码解析
一.example逻辑伪码 myRTSPClient附带3个example程序:simple_example.complete_example.common_example.后两个example都是从 ...
- js基础提高(二)
JavaScript基础提高(二) 上篇写的是JavaScript的历史.基本的数据类型和基本的语法进填的话讲的就深入一些了. js的函数 1.js函数定义的方式 (1)普通方式 语法:functio ...
- 【LCT】一步步地解释Link-cut Tree
简介 Link-cut Tree,简称LCT. 干什么的?它是树链剖分的升级版,可以看做是动态的树剖. 树剖专攻静态树问题:LCT专攻动态树问题,因为此时的树剖面对动态树问题已经无能为力了(动态树问题 ...
- Spring框架IOC,DI概念理解
1.什么是框架? 框架是一种重复使用的解决方案,针对某个软件开发的问题提出的. Spring框架,它是一个大型的包含很多重复使用的某个领域的解决方案. Spring的理念:不要重复发明轮子. 2.Sp ...
- tensorflow笔记(五)之MNIST手写识别系列二
tensorflow笔记(五)之MNIST手写识别系列二 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7455233.html ...
- 转每天一个linux命令(5):rm 命令
昨天学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所 ...
- ThreadLoacl的反思
在我的随笔 spring mvc:注解@ModelAttribue妙用 中使用ThreadLocal来简化spring mvc控制层controller中的ModelMap,Response.Jso ...
- zbrush曲面增加厚度
把曲面增加厚度方便雕刻机雕刻. 可以使用zbrush中的边循环功能. 1.准备好需要增加厚度的曲面,把曲面的边缘调整好,尽量的变得平滑. 2.将模型导入到zbrush中,开启双面显示,以方便观察模型的 ...
- ORACLE分区表、分区索引详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt160 ORACLE分区表.分区索引ORACLE对于分区表方式其实就是将表分段 ...
- 聊一聊我们都熟知的 “ Java分层 ”
一.为什么要分层. 以前的我们,写代码的时候,都在main()方法中,出现了错误,就慢慢调试,这样浪费了我们很长的时间,而我们程序员的时间是非常宝贵的 但是当我们使用分层架构的时候,就可以清晰明确的知 ...