Hibernate+spring缓存机制配置

在applicationContext.xml文件中添加以下代码:

<prop key= "hibernate.cache.use_second_level_cache" > true </prop>   <!--设置缓存机制为二级缓存 -->
<prop key= "hibernate.cache.use_query_cache" > true </prop>          <!--启动查询缓存 -->
<prop key= "hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</prop>  <!--设置二级缓存的Provider类 -->
<prop key= "hibernate.cache.provider_configuration_file_resource_path" >WEB-INF/classes/ehcache.xml</prop>   <!--设置缓存的配置文件路径 -->
                

 将ehcache.xml文件放到src下面,并配置ehcache.xml文件如下:

复制代码
<ehcache>
<diskStore path="D:\cache" />

<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600"
overflowToDisk="true" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cache name="net.nk.entity.DataAttr"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.SmContentCategory"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.SmProduct"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.SmDepartment"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.SmUser"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.DataAttrValue"
maxElementsInMemory="3000" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.DataAm"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.DataAttrGroup"
maxElementsInMemory="400" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
</ehcache>
复制代码

    hbm文件查找cache方法名的策略:如果不指定hbm文件中的region="ehcache.xml中的name的属性值",则使用name名为com.hoo.hibernate.entity.User的cache,如果不存在与类名匹配的cache名称,则用 defaultCache。 
如果User包含set集合,则需要另行指定其cache,例如User包含citySet集合,则需要添加如下配置到ehcache.xml中 

如果你使用了Hibernate的查询缓存,需要在ehcache.xml中加入下面的配置:

<cache name="org.hibernate.cache.UpdateTimestampsCache" 
    maxElementsInMemory="5000"  
    eternal="true"  
    overflowToDisk="true" /> 


<cache name="org.hibernate.cache.StandardQueryCache" 
    maxElementsInMemory="10000"  
    eternal="false"  
    timeToLiveSeconds="120" 
    overflowToDisk="true" /> 


在设置了缓存机制的类的xml文件中添加一段代码<cache usage="read-write" region="net.nk.entity.SmProduct"代码如下:

复制代码
<hibernate-mapping>
<!--SM_PRODUCT表的hibernate映射描述文件 -->
<class name="net.nk.entity.SmProduct" table="SM_PRODUCT" >
<cache usage="read-write" region="net.nk.entity.SmProduct"/>
<!-- ID -->
<id name="id" type="string">
<column name="ID" />
<generator class="assigned" />
</id>
复制代码

其中类的xml文件中的region设置要和ehcache.xml文件中的保持一致,此处是通过包名+类名的方式。

在调用数据库数据方法时,可采取以下设置:

复制代码
protected List<POJO> getAll(Class<T> entityClass,boolean iscache) throws SSHException {
HibernateTemplate ht = getHibernateTemplate();
if(iscache){
ht.setCacheQueries(true);
}
return ht.find("from "+entityClass.getName());//getHibernateTemplate().loadAll(entityClass);
}
复制代码


至此,可以通过放开hibernate的show_sql来查看是否缓存机制生效了


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值