hibernate二级缓存

本文介绍Hibernate二级缓存的配置及使用方法,包括EHCache的配置步骤和策略选择,适用于提高读取密集型应用的性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hibernate二级缓存:

   hibernate二级缓存成为进程级缓存或SessionFactory级缓存,二级缓存可以被所有session共享二级缓存的生命周期和SessionFactory的生命周期一致,SessionFactory可以管理二级缓存
  二级缓存的配置和使用(以EHCache配置为例):

      1. 建立ehcache.xml,如下:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
	<diskStore path="E:\\hibernate_cache"/>
	
	<defaultCache
		maxElementsInMemory="500"
		eternal="false"
		timeToIdleSeconds="100"
		timeToLiveSeconds="100"
		overflowToDisk="true"
	/>
	<cache name="com.hibernate.Student"
		maxElementsInMemory="500"
		eternal="false"
		timeToIdleSeconds="100"
		timeToLiveSeconds="100"
		overflowToDisk="true"
	/>
</ehcache>

     2.在hibernate.cfg.xml文件中加入缓存产品提供商

     3.

<propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    * 启用二级缓存,这也是它的默认配置

    4.<property name="hibernate.cache.use_second_level_cache">true</property>
      * 指定哪些实体类使用二级缓存

    5.可以在映射文件中采用<cache>标签指定或在hibernate.cfg.xml文件中统一指定
    注意使用的策略,通常采用read-only和read-write
    缓存原则:通常读远远大于写的数据进行缓存

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值