hibernate 二级缓存;
* 二级缓存 sessionFactory级的缓存 session之间可以共享(可插拔)
* 二级缓存的环境搭配如下:
1、拷贝hibernate包内etc文件夹内的\ehcache.xml目录(在src下加入ehcache.xml) 删除不必要的内容 只留下
<diskStore path="java.io.tmpdir"/> //溢出后存放的目录例如:<diskStore path="d:\\cache"/>
<defaultCache
maxElementsInMemory="10000" //在缓存区最大元素 默认为10000;
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true" //溢出到硬盘;
/>
<cache name="pojo.Items" //要管理的类,如果有多个类需要管理的话 可以复制创建多个cache;
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true" //溢出到硬盘;
/>
2、在hibernate.cfg.xml中添加如下:
<property name="hibernate.cache.use_second_level_cache">true</property> //为true的时候是启用二级缓存
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> //指明提供商
<class-cache class="pojo.User" usage="read-only"/> //管理二级缓存的类,如果有多个类需要管理 可创建多个;
* 二级缓存 sessionFactory级的缓存 session之间可以共享(可插拔)
* 二级缓存的环境搭配如下:
1、拷贝hibernate包内etc文件夹内的\ehcache.xml目录(在src下加入ehcache.xml) 删除不必要的内容 只留下
<diskStore path="java.io.tmpdir"/> //溢出后存放的目录例如:<diskStore path="d:\\cache"/>
<defaultCache
maxElementsInMemory="10000" //在缓存区最大元素 默认为10000;
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true" //溢出到硬盘;
/>
<cache name="pojo.Items" //要管理的类,如果有多个类需要管理的话 可以复制创建多个cache;
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true" //溢出到硬盘;
/>
2、在hibernate.cfg.xml中添加如下:
<property name="hibernate.cache.use_second_level_cache">true</property> //为true的时候是启用二级缓存
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> //指明提供商
<class-cache class="pojo.User" usage="read-only"/> //管理二级缓存的类,如果有多个类需要管理 可创建多个;