Spring+Hibernate3.6.9通过EhCache配置缓存

可到 http://www.ehcache.org/downloads/catalog 下载EhCache

1,applicationContext.xml配置

           <prop key="hibernate.show_sql">true</prop>
              <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>

 2,修改要使用缓存的映射类.hbm.xml文件:

 

<class name="com.db.MyTable" table="my_table">
        <cache usage="nonstrict-read-write" />

 <cache>必须紧跟<class>,usage 有read-write,read-only等选项,具体的请Google

3,在源文件根目录下新建ehcache.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <diskStore path="D:\\temp" />
    <defaultCache maxElementsInMemory="10000" eternal="false"
                  timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />
    <cache name="com.db.MyTable" maxEntriesLocalHeap="10000"
           eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
           overflowToDisk="true" />
</ehcache>

 重启应用,如果发现第一次查询MyTable时打印的有sql语句,第二次没有就证明成功了

猜你喜欢

转载自wowtianwen.iteye.com/blog/2105305