Hibernate Cache

本文详细介绍了Hibernate中的一级缓存(Session级别)和二级缓存(SessionFactory级别)的工作原理及配置方法。一级缓存用于同一Session内多次查询相同对象的情况,而二级缓存则适用于数据集有限且修改频率低的数据,例如用户授权信息等。

1. First level Cache--Session.

Querying of same object multiple times within a same session will only call DB for once, the rest of times will use cache to retrieve.

Same to openSession()&getCurrentSession()

 

2. Second level Cache--SessionFactory.

Data that is good for second level cache:

1.limited set of data;

2. Rarely being modified;

3. Won't be access by multiple objects at the same time

Example:

user authorization info. User entries are limited.  load()&iterative() use secondary cache; list uses second level cache,however, query doesn't!!

 

When hibernate accesses data, it first check first level cache, then check second level cache if it's configured, then check DB.

Configuration:

1.add ehcache.xml

2.

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class"> org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

 

3. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)  ----> use default cache

 

For query checking using second level:

<property name="cache.use_query_cache">true</property>

 

xx.createQuery().setCachable(true).list 

 

转载于:https://www.cnblogs.com/fifi043/p/4914164.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值