hibernate的缓存

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <diskStore path="d:/cache/"/>

    <!--
    Mandatory Default Cache configuration. These settings will be applied to caches
    created programmtically using CacheManager.add(String cacheName)
    -->
    <!--
       name:缓存名称。
       maxElementsInMemory:缓存最大个数。
       eternal:对象是否永久有效,一但设置了,timeout将不起作用。
       timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
       timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
       overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
       diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
       maxElementsOnDisk:硬盘最大缓存个数。
       diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
       diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
       memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
       clearOnFlush:内存数量最大时是否清除。
    -->
    <defaultCache
            maxElementsInMemory="1000"
            overflowToDisk="true"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            />
</ehcache>

 

 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
        <property name="dataSource"> 
           <ref bean="dataSource" /> 
        </property> 
        <property name="hibernateProperties"> 
        	<value>
        		hibernate.show_sql=true
        		hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
		     hibernate.cache.use_query_cache=true
		        hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
        	
        	</value>
        </property>
         <property name="mappingResources"> 
         	<list>
	         	<value>net/spring/bo/User.hbm.xml</value>
	         	<value>net/spring/bo/Address.hbm.xml</value> 
	         	<value>net/spring/bo/ClassName.hbm.xml</value>
	         	<value>net/spring/bo/Student.hbm.xml</value> 
         	</list>
         </property>
	 </bean> 

 

众所周知hibernate有两级缓存,分别为session级别和sessionFactory级别。session级别缓存为一级缓存,只能一个线程用。在hibernate中默认是打开的,也就是我们无须打开。现在主要讨论hibernate的二级缓存,即sessionFactory级别缓存。

sessionFactory是默认关闭的。打开二级缓存需要在hibernate配置文件或者Spring(如果是配置了Spring框架)中打开。以applicationContext为例。

hibernate.cache.use_query_cache=true是打开二级缓存。 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider为使用二级缓存的种类。hibernate二级缓存种类有好几种方法。

然后在bo类的配置文件中还需要 <cache usage="read-only"/> 只读缓存

然后再次两个不同的Session查询get或者load的时候只会出现一条sql语句。即可以证明是从查询中缓存出来这就是类缓存

hibernate的缓存默认的是智能根据Id查询。如果使用iterate则会出现N+1的问题。hibernate有查询缓存即 List it = s1.createQuery("From User u where u.id <10")
  .setCacheable(true)
  .list();

设置为true即可放入放入缓存中即查询缓存。但是如果查询条件变了查询缓存就不起作用。如果From User u where u.id <8。缓存就不起作用。只要查询条件变化,缓存就不起作用,实际项目意义不大感觉。

利用update和delete使用hql语句会存在脏对象问题,不会通知Session缓存。二级缓存会通知么?

先get一个数据,再用HQL语句更新一个对象。结果会通知二级缓存,我们不用担心脏数据问题。这就是hibernate的更新时间戳缓存。

一般建议使用第三方缓存框架。如EHCache需要把配置文件修改为 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider。

 

posted @ 2014-03-18 16:13  942391815  阅读(181)  评论(0编辑  收藏  举报