1.什么是缓存
一级缓存:session级别的缓存
二级缓存:sessionFactior缓存,保存所有session
什么时候用?
1.存放经常被访问的对象
2.改动不大
3.数量不大
4.不会被并发访问,允许出现偶尔并发的数据
5.参考数据
注意:
1.load iterate 默认使用二级缓存
2.list默认向二级缓存内添加数据,但是查询的时候不使用二级缓存
查询缓存(三级缓存):即用方法查询缓存中的内容(比如,二级缓存中有三个用户,我要查询缓存中的第二个用户)
使用方法:
1.在hibernate.cfg.xml配置文件中添加(依赖于二级缓存,也就是说,下面的配置还是要的)
<property name="cache.use_query_cache">true</property>
2.调用Query的setCachable(true)方法使用二级缓存
List<District> list = session.createQuery("from Users").setCacheable(true).list();
2.hibernate的配置(EhCache)
1.hibernate.cfg.xml
打开二级缓存: <property name="hibernate.cache.use_second_level_cache">true</property>
设置二级缓存的工具: <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
2.Entity.hbm.xml(需要二级缓存的类)
在实体对象的映射文件中(方案一)
<cache usage="read-only"/>
read-only 只读
transactional 事务缓存
read-write 读写
nonstrict-read-write 非严格读/写缓存
read-only 只写
注意:
1.必须写在<class>标签内部
2.必须写在<id>标签前面
在hibernate.cfg.xml配置文件中(方案二)
<class-cache class="com.bjsxt.hibernate.Classes" usage="read-only" />
注意:必须写在<mapping>标签后面
3.@Cache
3.eheache.xml配置
<ehcache>
<!-- 缓存保存到硬盘上的位置,现在设置的是保存到系统默认的缓存路径中 -->
<diskStore path="java.io.tmpdir"/>
<defaultCache <!-- 默认的缓存-->
maxElementsInMemory="10000" <!-- 最多储存多少个对象-->
eternal="false" <!-- 是否是不可更改的-->
timeToIdleSeconds="120" <!-- 无人访问时缓存存在的时间-->
timeToLiveSeconds="1200" <!-- 缓存存在的时间-->
overflowToDisk="true" <!-- 是否放到硬盘上-->
/>
<cache name="sampleCache1" <!-- name属性,方便使用时决定调用哪个缓存配置-->
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
</ehcache>
4.缓存算法
作用:如果缓存满了,指定替换的对象
LRU (Least Recently Used) 最近很少使用 即:那个对象被访问的时间离现在最远,谁被替换
LFU (Least Frequently Used) 最近不常被使用 即:使用次数最少的被替换
FIFO (First In First Out) 第一个进,第一个出 不解释
使用方法:
在eheache.xml 中添加配置: memoryStoreEvictionPolicy="LRU"
一级缓存:session级别的缓存
二级缓存:sessionFactior缓存,保存所有session
什么时候用?
1.存放经常被访问的对象
2.改动不大
3.数量不大
4.不会被并发访问,允许出现偶尔并发的数据
5.参考数据
注意:
1.load iterate 默认使用二级缓存
2.list默认向二级缓存内添加数据,但是查询的时候不使用二级缓存
查询缓存(三级缓存):即用方法查询缓存中的内容(比如,二级缓存中有三个用户,我要查询缓存中的第二个用户)
使用方法:
1.在hibernate.cfg.xml配置文件中添加(依赖于二级缓存,也就是说,下面的配置还是要的)
<property name="cache.use_query_cache">true</property>
2.调用Query的setCachable(true)方法使用二级缓存
List<District> list = session.createQuery("from Users").setCacheable(true).list();
2.hibernate的配置(EhCache)
1.hibernate.cfg.xml
打开二级缓存: <property name="hibernate.cache.use_second_level_cache">true</property>
设置二级缓存的工具: <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
2.Entity.hbm.xml(需要二级缓存的类)
在实体对象的映射文件中(方案一)
<cache usage="read-only"/>
read-only 只读
transactional 事务缓存
read-write 读写
nonstrict-read-write 非严格读/写缓存
read-only 只写
注意:
1.必须写在<class>标签内部
2.必须写在<id>标签前面
在hibernate.cfg.xml配置文件中(方案二)
<class-cache class="com.bjsxt.hibernate.Classes" usage="read-only" />
注意:必须写在<mapping>标签后面
3.@Cache
3.eheache.xml配置
<ehcache>
<!-- 缓存保存到硬盘上的位置,现在设置的是保存到系统默认的缓存路径中 -->
<diskStore path="java.io.tmpdir"/>
<defaultCache <!-- 默认的缓存-->
maxElementsInMemory="10000" <!-- 最多储存多少个对象-->
eternal="false" <!-- 是否是不可更改的-->
timeToIdleSeconds="120" <!-- 无人访问时缓存存在的时间-->
timeToLiveSeconds="1200" <!-- 缓存存在的时间-->
overflowToDisk="true" <!-- 是否放到硬盘上-->
/>
<cache name="sampleCache1" <!-- name属性,方便使用时决定调用哪个缓存配置-->
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
</ehcache>
4.缓存算法
作用:如果缓存满了,指定替换的对象
LRU (Least Recently Used) 最近很少使用 即:那个对象被访问的时间离现在最远,谁被替换
LFU (Least Frequently Used) 最近不常被使用 即:使用次数最少的被替换
FIFO (First In First Out) 第一个进,第一个出 不解释
使用方法:
在eheache.xml 中添加配置: memoryStoreEvictionPolicy="LRU"