Hibernate缓存策略(一级缓存和EHcache二级缓存)
如何配置二级缓存:
第一步:导入EHcache依赖
1)Maven项目:
<!--此处使用hibernate4-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.3.10.Final</version>
</dependency>
2)普通项目:
解压下载的hibernate压缩包,找到路径:\hibernate-release-4.3.10.Final\lib\optional\ehcache,把路径下的包全部导入到项目
第二步:在源文件夹下,创建ehcache.xml
<ehcache>
<!--
磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
path:指定在硬盘上存储对象的路径
path可以配置的目录有:
user.home(用户的家目录)
user.dir(用户当前的工作目录)
java.io.tmpdir(默认的临时目录)
ehcache.disk.store.dir(ehcache的配置目录)
绝对路径(如:d:\\ehcache)
查看路径方法:String tmpDir = System.getProperty("java.io.tmpdir");
-->
<diskStore path="java.io.tmpdir" />
<!--
defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
eternal:代表对象是否永不过期 (指定true则下面两项配置需为0无限期)
timeToIdleSeconds:最大的发呆时间 /秒
timeToLiveSeconds:最大的存活时间 /秒
overflowToDisk:是否允许对象被写入到磁盘
说明:下列配置自缓存建立起600秒(10分钟)有效 。
在有效的600秒(10分钟)内,如果连续120秒(2分钟)未访问缓存,则缓存失效。
就算有访问,也只会存活600秒。
-->
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="600" overflowToDisk="true" />
<!-- (此处为非必需的配置,对应第四步)
cache:为指定名称的对象进行缓存的特殊配置
name:指定对象的完整名
-->
<cache
name="StudentCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
</ehcache>
第三步:在hibernate.cfg.xml中配置
<!-- 配置二级缓存 -->
<!-- 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>
如果是Spring+Hibernate,需要在spring.xml中
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- 方言 -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- 更新表结构:有表使用无表创建 -->
<!-- 开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- 启用查询缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- 配置二级缓存提供商 -->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<!-- 加载缓存所需配置文件 -->
<prop key="hibernate.net.sf.ehcache.configurationResourceName">classpath:ehcache.xml</prop>
</props>
</property>
...
</bean>
第四步:在需要缓存的对象中开启
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-9-24 20:49:54 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zyzpp.hibernate.Students" table="students">
<!-- 开启Ehcache缓存 -->
<!-- usage:缓存策略
region:指定缓存配置(需单独在ehcache.xml配置)
include:是否缓存延迟加载的对象(如name,sex,number)
-->
<cache usage="read-only" include="all" region="StudentCache"/>
<id name="id" type="int">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" />
</property>
<property name="number" type="int">
<column name="number" />
</property>
</class>
</hibernate-mapping>
若是使用hibernate注解:@Cache(usage =CacheConcurrencyStrategy.READ_ONLY)
@Entity
@Table(name = "EMPLOYEE")
@Cache(usage =CacheConcurrencyStrategy.READ_ONLY,include="all", region="") //开启缓存
public class Employee {
@Id //定义主键
@GeneratedValue(strategy=GenerationType.IDENTITY) //主键生成策略:自动选择mysql自增
@Column(name = "id") //对应数据库字段
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "salary")
private int salary;
public Employee() {
//@Entity规定必须有的构造方法
}
//....getter
//....setter
}
Hibernate缓存策略(一级缓存和EHcache二级缓存)的更多相关文章
- hibernate ehcache二级缓存
xml配置 <?xml version="1.0" encoding="UTF-8"?> <ehcache> <!-- Sets ...
- mybatis缓存,包含一级缓存与二级缓存,包括ehcache二级缓存
一,引言 首先我们要明白一点,缓存所做的一切都是为了提高性能.明白了这一点下面我们开始进入正题. 二,mybatis缓存概要 ①.mybatis的缓存有两种,分别是一级缓存和二级缓存.两者都属于查询缓 ...
- hibernate学习(9)——日志,一对一,二级缓存
1.Hibernate中的日志 1 slf4j 核心jar : slf4j-api-1.6.1.jar .slf4j是日志框架,将其他优秀的日志第三方进行整合. 整合导入jar包 log4j 核心 ...
- ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存
ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存 hibernate : Hibernate是一个持久层框架,经常访问物理数据库 ...
- hibernate学习笔记第七天:二级缓存和session管理
二级缓存配置 1.导入ehcache对应的三个jar包 ehcache/*.jar 2.配置hibernate使用二级缓存 2.1设置当前环境开始二级缓存的使用 <property name=& ...
- MyBatis ehcache二级缓存
ehcache二级缓存的开启步骤: 1.导入jar 2.在映射文件中指定用的哪个缓存 3.加一个配置文件,这个配置文件在ehcache jar包中就有 使增删改对二级缓存不刷新: 对一级缓存没有用的, ...
- Hibernate JPA 中配置Ehcache二级缓存
在Hibernate3 JPA里配置了一下非分布式环境的二级缓存,效果不错.具体过程如下: 1, 需要引入的jar包 http://ehcache.org/downloads/catalog 下载的包 ...
- MyBatis笔记——EhCache二级缓存
介绍 ehcache是一个分布式缓存框架. 我们系统为了提高系统并发,性能.一般对系统进行分布式部署(集群部署方式) 不使用分布缓存,缓存的数据在各各服务单独存储,不方便系统开发.所以要使用分布式缓 ...
- 使用Spring Ehcache二级缓存优化查询性能
最近在对系统进行优化的时候,发现有些查询查询效率比较慢,耗时比较长, 通过压测发现,主要耗费的性能 消耗在 查询数据库,查询redis 数据库:连接池有限,且单个查询不能消耗大量的连接池,占用大量IO ...
随机推荐
- python 冒泡、二分查找
冒泡: import random def _sort(_lst): count = 1 while count < len(_lst): for i in range(0, len(_lst) ...
- x3D 下载以及如何使用原版NetBeans IDE 来搭建x3d编辑环境
安装前: Overview X3D-Edit version 3.3 standalone application and Netbeans plugin are available and read ...
- java笔记----常见的异常
常见的可控异常 运行时的异常 异常信息的获取
- 原生js :removeClass和addClass
function removeClass(obj, aClass) { var re = new RegExp('\\b' + aClass + '\\b'); if (obj.className ! ...
- C# Modbus协议中读取浮点数的操作方法
输入参数P1,P2代表PLC中浮点数储存的两个寄存器获取的数据 public static float GetFloat(ushort P1, ushort P2) { int intSign, in ...
- Linux下2号进程的kthreadd--Linux进程的管理与调度(七)
2号进程 内核初始化rest_init函数中,由进程 0 (swapper 进程)创建了两个process init 进程 (pid = 1, ppid = 0) kthreadd (pid = 2, ...
- AI学习---特征工程【特征抽取、特征预处理、特征降维】
学习框架 特征工程(Feature Engineering) 数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已 什么是特征工程: 帮助我们使得算法性能更好发挥性能而已 sklearn主 ...
- shell 关于路径查询显示pwd
获取根目录:dirname $0 cd到根目录:cd `dirname $0` 获取当前目录:pwd 因此,要获取当前目录应该是: cd `dirname $` && pwd 或者 $ ...
- Ecto 总结
ecto 简介 ecto 相当于 elixir 的 ORM,但是得益于 elixir 语言,和传统的 ORM 相比,更加简洁和强大. ecto 主要分为 4 部分: Repo: 这是和真正数据库交互的 ...
- SAP S/4嵌入式分析——虚拟数据模型(VDM)
在本文中,我会通过CDS视图来介绍虚拟数据模型(Virtual Data Model,以下简称VDM). 在SAP HANA平台出现后,SAP的业务应用开发模式已经产生了变化,新的经验法则是:尽可能在 ...