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
缓存原则:通常读远远大于写的数据进行缓存