hibernate之二级缓存

本文介绍了Hibernate的二级缓存概念及其重要性,解释了一级和二级缓存的区别。通过实例展示了Ehcache作为二级缓存框架的基本使用,并详细讲解了缓存原理,包括如何配置和管理缓存。文章还通过代码演示了在Hibernate中配置和使用Ehcache,以及如何通过调整配置实现数据的缓存策略。
摘要由CSDN通过智能技术生成

什么是二级缓存?

Hibernate提供了一级缓存和二级缓存,合理的利用缓存可以有助于提高系统的性能,为了避免不合理的利用缓存导致内存过度消耗降低系统性能,可以通过合理配置缓存的参数来避免这个问题。

Hibernate中的一级缓存由Session管理,二级缓存由SessionFactory来管理。在使用时,二级缓存是可有可无的,但一级缓存是必不可少的。

在通常情况下会将具有以下特征的数据放入到二级缓存中:

● 很少被修改的数据。
● 不是很重要的数据,允许出现偶尔并发的数据。
● 不会被并发访问的数据。
● 参考数据。

介绍下数据库:
关系型数据库:数据与数据之间存在关系(联系)的数据库 mysql/Oracle、sqlserver
非关系型数据库:数据与数据之间是不存在关系的,key-value

	1、基于文件存储的数据库:ehcache
	2、基于内存存储的数据库:redis、memcache
	3、基于文档存储的数据库:mongodb

今天主要介绍ehcache和hibernate结合实现二级缓存,Ehcache 是现在最流行的纯Java开源缓存框架,配置简单、结构清晰、功能强大

先带正好在看这篇博客的你简单了解下什么是ehcahe
提供项目大体图片
在这里插入图片描述
下面开始撸代码:
提供pom.xml文件(导入依赖才能使用)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zking</groupId>
  <artifactId>hidernate</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>hidernate Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>

		<junit.version>4.12</junit.version>
		<servlet.version>4.0.0</servlet.version>
		<hibernate.version>5.2.12.Final</hibernate.version>
		<mysql.driver.version>5.1.46</mysql.driver.version>

		<ehcache.version>2.10.0</ehcache.version>
		<slf4j-api.version>1.7.7</slf4j-api.version>
		<log4j-api.version>2.9.1</log4j-api.version>
	</properties>
  
 <dependencies>
  <dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.driver.version}</version>
		</dependency>
		
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
			<version>${ehcache.version}</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		
		<!-- slf4j核心包 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j-api.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${slf4j-api.version}</version>
			<scope>runtime</scope>
		</dependency>

		<!--用于与slf4j保持桥接 -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>${log4j-api.version}</version>
		</dependency>

		<!--核心log4j2jar包 -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>${log4j-api.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>${log4j-api.version}</ver
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值