hibernate开启二级缓存

开启二级缓存需要导入三个jar包,还有一个配置文件到资源根目录下

ehcache.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> 	
	<!-- 缓存数据要存放的磁盘地址 -->
	<diskStore path="java.io.tmpdir" /> 
	<!-- diskStore:指定数据在磁盘中的存储位置。 ? defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略 
		以下属性是必须的: ? maxElementsInMemory - 在内存中缓存的element的最大数目 ? maxElementsOnDisk 
		- 在磁盘上缓存的element的最大数目,若是0表示无穷大 ? eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断 
		? overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上 以下属性是可选的: ? timeToIdleSeconds 
		- 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大 
		? timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大 diskSpoolBufferSizeMB 
		这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区. ? diskPersistent 
		- 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。 ? diskExpiryThreadIntervalSeconds 
		- 磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作 ? memoryStoreEvictionPolicy 
		- 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出) -->

	<defaultCache maxElementsInMemory="1000"
		maxElementsOnDisk="10000000" eternal="false" overflowToDisk="false"
		timeToIdleSeconds="120" timeToLiveSeconds="120"
		diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
	</defaultCache>

	 <!-- 
		name: cache的名字,用来识别不同的cache,必须惟一。   
		maxElementsInMemory: 内存管理的缓存元素数量最大限值。   
		maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。   
		eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。   
		overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。
		timeToIdleSeconds: 设定元素在过期前空闲状态的时间,只对非持久性缓存对象有效。默认值为0,值为0意味着元素可以闲置至无限长时间。   
		timeToLiveSeconds: 设定元素从创建到过期的时间。其他与timeToIdleSeconds类似。   
		diskPersistent: 设定在虚拟机重启时是否进行磁盘存储,默认为false.(我的直觉,对于安全小型应用,宜设为true)。   
		diskExpiryThreadIntervalSeconds: 访问磁盘线程活动时间。   
		diskSpoolBufferSizeMB: 存入磁盘时的缓冲区大小,默认30MB,每个缓存都有自己的缓冲区。   
		memoryStoreEvictionPolicy: 元素逐出缓存规则。共有三种,Recently Used (LRU)最近最少使用,为默认。 First In First Out (FIFO),先进先出。Less Frequently Used(specified as LFU)最少使用  
	-->
	<cache name="userCache" maxElementsInMemory="3000" eternal="false"
		overflowToDisk="true" timeToIdleSeconds="3600" timeToLiveSeconds="3600"
		memoryStoreEvictionPolicy="LFU" />

</ehcache>

 hibernate.cfg.xml:

注意:开启缓存要写在mapping映射下方    里面的集合缓存意思是缓存orders的id还是需要把Orders这个类也进行缓存 

在test中测试一级缓存和二级缓存的区别:

//一级缓存缓存的是对象本身
Customer customer=(Customer) session.get(Customer.class, 1);
Customer customer2=(Customer) session.get(Customer.class,1);
System.out.println("customer1:"+customer);
System.out.println("customer2:"+customer2);
tx.commit();
session.close();
//二级缓存只缓存数据
Session session2=factory.openSession();
Transaction tx2=session2.beginTransaction();
Customer customer3=(Customer) session2.get(Customer.class,1);
System.out.println("customer3:"+customer3);
System.out.println(ReflectionToStringBuilder.toString(customer3));

查询的sql语句只执行了一次

  

customer3.getOrders().forEach(orders->{
		System.out.println(ReflectionToStringBuilder.toString(orders));
});

就算取出orders,只执行一次查询

没有缓存Orders类的话,需要执行多次

猜你喜欢

转载自blog.csdn.net/weixin_36328444/article/details/81385036