• 【Hibernate 7】浅谈Hibernate的缓存机制


    一、Hibernate缓存机制简介

    对于Hibernate本身来说,它的缓存主要包括三部分:session缓存(一级缓存)、二级缓存、查询缓存。

    1.1,session缓存

    随着session的关闭而消失,load、iterator操作,会从一级缓存中查找数据,如果找不到,再到数据库里面查找。Query.list操作,如果没有配置查询缓存,将直接从数据库中获取数据。

    特点:只能被当前事务访问,每个事务都有各自的缓存,缓存内的数据通常采用相互关联的对象形式.缓存的生命周期依赖于事务的生命周期,只有当事务结束时,缓存的生命周期才会结束。事务范围的缓存使用内存作为存储介质,一级缓存属于事务范围。

    1.2,二级缓存

    hibernate并没有提供相应的二级缓存的组件,所以需要加入额外的二级缓存包,常用的二级缓存包是EHcache。这个我们在下载好的hibernate的lib下可以找到,然后将里面的几个jar包导入即可。

    为什么要有二级缓存:

    一级缓存是session级别,也就是事务级别的。当session关闭后,一级缓存就不存在了,当再次需要获取数据的时候,需要再次发送sql语句。为了解决session关闭,缓存失效的问题,我们配置二级缓存。

    特点:

    可以被应用范围内的所有事务共享访问,缓存的生命周期依赖于应用的生命周期,只有当应用结束时,缓存的生命周期才会结束。应用范围的缓存可以使用内存或硬盘作为存储介质,二级缓存属于应用范围。

    1.3,查询缓存

    所谓查询缓存,即让hibernate缓存list、iterator、createQuery等方法的查询结果集。如果没有打开查询缓存,hibernate将只缓存load方法获得的单个持久化对象。在打开了查询缓存之后,需要注意,调用query.list()操作之前,必须显式调用query.setCachable(true)来标识某个查询使用缓存。


    二、实例分析二级缓存

    2.1,配置二级缓存

    首先,由于Hibernate的二级缓存是通过使用第三方包encache实现的,所以我们需要配置一个encache.xml的文件,来配置我们的缓存信息,并将这个文件放在项目根目录下

    <span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;"><ehcache>
    
        <!-- Sets the path to the directory where cache .data files are created.
    
             If the path is a Java System Property it is replaced by
             its value in the running VM.
    
             The following properties are translated:
             user.home - User's home directory
             user.dir - User's current working directory
             java.io.tmpdir - Default temp file path -->
      
      <!--指定二级缓存存放在磁盘上的位置-->
        <diskStore path="user.dir"/>  
    
      <!--我们可以给每个实体类指定一个对应的缓存,如果没有匹配到该类,则使用这个默认的缓存配置-->
        <defaultCache
            maxElementsInMemory="10000"  //在内存中存放的最大对象数
            eternal="false"         //是否永久保存缓存,设置成false
            timeToIdleSeconds="120"    
            timeToLiveSeconds="120"    
            overflowToDisk="true"     //如果对象数量超过内存中最大的数,是否将其保存到磁盘中,设置成true
            />
      
      <!--
        1、timeToLiveSeconds的定义是:以创建时间为基准开始计算的超时时长;
        2、timeToIdleSeconds的定义是:在创建时间和最近访问时间中取出离现在最近的时间作为基准计算的超时时长;
        3、如果仅设置了timeToLiveSeconds,则该对象的超时时间=创建时间+timeToLiveSeconds,假设为A;
        4、如果没设置timeToLiveSeconds,则该对象的超时时间=max(创建时间,最近访问时间)+timeToIdleSeconds,假设为B;
        5、如果两者都设置了,则取出A、B最少的值,即min(A,B),表示只要有一个超时成立即算超时。
    
      -->
    
      <!--可以给每个实体类指定一个配置文件,通过name属性指定,要使用类的全名-->
        <cache name="com.angel.hibernate.Student"
            maxElementsInMemory="100"
            eternal="false"
            timeToIdleSeconds="10000"
            timeToLiveSeconds="10000"
            overflowToDisk="true"
            />
    
        <cache name="sampleCache2"
            maxElementsInMemory="1000"
            eternal="true"
            timeToIdleSeconds="0"
            timeToLiveSeconds="0"
            overflowToDisk="false"
            /> -->
    
    
    </ehcache></span></span>

    其次,配置Hibernate.cfg.xml文件,启用二级缓存

    <span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;"></pre><pre name="code" class="html">	<!-- 配置缓存提供商 -->
    	<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    	<!--Hibernate4以上的提供商配置	
    	<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
    	-->
    	<!-- 启用二级缓存,这也是它的默认配置 -->
    	<property name="hibernate.cache.use_second_level_cache">true</property>
    	
    	<!-- 二级缓存配置文件的位置 -->
            <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>
    
    	<!-- 指定Student使用二级缓存 -->
    	<class-cache class="com.angel.hibernate.Student" usage="read-only"/></span></span>

    2.2,配置实体映射文件的缓存

    <span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
    	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    	<class name="com.bjpowernode.hibernate.Student" table="t_student">
    		<!-- 二级缓存一般设置为只读的 -->
    		<cache usage="read-only"/>
    		<id name="id">
    			<generator class="native"/>
    		</id>
    		<property name="name"/>
    		<many-to-one name="classes" column="classesid"/>
    	</class>
    </hibernate-mapping></span></span>

    2.3,实例测试二级缓存

    <span style="font-family:KaiTi_GB2312;font-size:18px;"><span style="font-family:KaiTi_GB2312;font-size:18px;">/**
    * 开启二级缓存
    * 
    * 在两个session中发load查询
    */
    public void testCache1() {
    	Session session = null;
    	try {
    		session = HibernateUtils.getSession();
    		session.beginTransaction();
    		Student student = (Student)session.load(Student.class, 1);
    		System.out.println("student.name=" + student.getName());
    		session.getTransaction().commit();
    	}catch(Exception e) {
    		e.printStackTrace();
    		session.getTransaction().rollback();
    	}finally {
    		HibernateUtils.closeSession(session);
    	}
    		
    	try {
    		session = HibernateUtils.getSession();
    		session.beginTransaction();
    		Student student = (Student)session.load(Student.class, 1);
    			
    		//不会发出查询语句,因为配置二级缓存,session可以共享二级缓存中的数据
    		System.out.println("student.name=" + student.getName());
    		session.getTransaction().commit();
    	}catch(Exception e) {
    		e.printStackTrace();
    		session.getTransaction().rollback();
    	}finally {
    		HibernateUtils.closeSession(session);
    	}</span></span>

    三、实例分析查询缓存

    首先,Hibernate.cfg.xml配置

    <span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- 启用查询缓存,默认是false是不起用的 -->
    <property name="hibernate.cache.use_query_cache">true</property></span>

    其次,在应用缓存是还需要开启查询缓存

    <span style="font-family:KaiTi_GB2312;font-size:18px;">List names = session.createQuery("select s.name from Student s").setCacheable(true).list();</span>

    注意:

    1,查询缓存,和session的生命周期没有联系,它同样属于应用程序级别的缓存

    2,使用query.iterate()查询普通的属性,将不会启动查询缓存,查询缓存只对query.list()起作用

    3,如果关闭二级缓存,只启用查询缓存,那么查询缓存会缓存实体对象的id,当第二次执行query.list的时候,将缓存中的id取出,分别到一级和二级缓存中查询相对应的实体,如果存在,则直接使用对象,否则发出查询的sql语句。而如果我们关闭二级缓存,那么查询时将不能再缓存中找到实体对象,则会发出N条查询子句,将再次引起N+1问题,所以在应用查询缓存的时候,一定要开启二级缓存。


    四、总结

    以上就是Hibernate中关于缓存机制的总结,但在实际应用中,我们仍然需要对缓存进行管理,比如:

    一级缓存的管理:

    1,evit(Object obj)  将指定的持久化对象从一级缓存中清除,释放对象所占用的内存资源,指定对象从持久化状态变为脱管状态,从而成为游离对象
    2,clear()  将一级缓存中的所有持久化对象清除,释放其占用的内存资源
    3,contains(Object obj) 判断指定的对象是否存在于一级缓存中
    4,flush() 刷新一级缓存区的内容,使之与数据库数据保持同步

    二级缓存的管理:

    1,evict(Class arg0, Serializable arg1)  将某个类的指定ID的持久化对象从二级缓存中清除,释放对象所占用的资源

    2,evict(Class arg0)  将指定类的所有持久化对象从二级缓存中清除,释放其占用的内存资源
    3,evictCollection(String arg0)  将指定类的所有持久化对象的指定集合从二级缓存中清除,释放其占用的内存资源

  • 相关阅读:
    wustctf2020_name_your_dog
    wdb2018_guess(stack smash,__environ获取栈变量)
    ciscn_2019_en_3
    植物大战僵尸逆向(秒杀僵尸)
    pwnable_hacknote
    pwn-hitcontraining_magicheap(unsorted bin attack)
    pwn-[XMAN]level5(通用gadget,mprotect)
    pwn-pwnable_orw(seccomp&orw)
    《心流》有感
    LC112 路径总和
  • 原文地址:https://www.cnblogs.com/hhx626/p/6010314.html
Copyright © 2020-2023  润新知