Hibernate的一级缓存和二级缓存sql
1,什么是缓存?缓存
2,什么是一级缓存?session
一级缓存,就是session级别的缓存ide
同一个session里,若是查询的数据存在,则直接在内存中取出,不发出sql语句spa
3,什么是二级缓存?hibernate
二级缓存,就是SessionFactory级别的缓存,能够跨越session存在orm
打开二级缓存xml
1,在hibernate.cfg.xml中设定对象
<property name=”cache.use_second_level”>true</property>内存
<property name=”cache.provider_class”>org.hibernate.cache.EhCacheProvider</property>
配置ehcache.xml配置文件
DefaultCache
maxElementsInMemory 最大对象个数
Eternal 永远生存 = true 能够替换缓存对象
timeToIdleSeconds = 120 120秒后没有使用,去掉
TimeToLiveSconds = 1200 1200生存时间后,去掉,生命值
OverflowToDisk=true 内存不够用,放在硬盘上
二级缓存适合的对象
常常被访问
改动不大,不会常常被改动
数量有限
(组织机构,权限,地区)
2,是用@cache注解,须要使用二级缓存的对象(org.hibernate.annotations)
@cache(usage=CacheConcurrencyStrategy.READ_ONLY) READ_WRITE(用的比较多)
4,load默认使用二级缓存,iterate 默认使用二级缓存
List默认往二级缓存加数据,可是查询的时候不会使用二级缓存
5,若是要query用二级缓存,须要打开查询缓存
查询缓存起做用的话,必须这个查询条件同样
I,<property name=”cache.use_query_cache”>true</property>
ii.调用Query的setCacheable(true)方法指明使用二级缓存