項目開發中的緩存問題(hibernate+spring)

爲了簡要總結,我就大概寫下自己的關鍵詞語吧,這個項目是數據庫訪問層用的是hibernate。

1.EhCache是什麼
    EhCache是Hibernate的二級緩存技術之一,可以把查詢出來的數據存儲在內存或者磁盤,節省下次同樣查詢語句再次查詢數據庫,大幅減輕數據庫壓力;

2.EhCache的使用注意點
    當用Hibernate的方式修改表數據(save,update,delete等等),這時EhCache會自動把緩存中關於此表的所有緩存全部刪除掉(這樣能達到同步)。但對於數據經常修改的表來說,可能就失去緩存的意義了(不能減輕數據庫壓力);

3.EhCache使用的場合
    3.1比較少更新表數據
        EhCache一般要使用在比較少執行write操作的表(包括update,insert,delete等)[Hibernate的二級緩存也都是這樣];
    3.2對併發要求不是很嚴格的情況
        兩臺機子中的緩存是不能實時同步的;

3.在項目中的實現

   一:導入ehcache-1.2.3.jar 到lib下。
   二:導入ehcache.xml到src下。
           這邊貼下ehcache.xml的內容:
<span style="color:#000000;"><?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!--timeToIdleSeconds 當緩存閒置n秒後銷燬 -->
<!--timeToLiveSeconds 當緩存存活n秒後銷燬 -->
<!--
	     緩存配置
       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:內存數量最大時是否清除。
--> 
	<diskStore path="java.io.tmpdir" />
	<defaultCache 
		maxElementsInMemory="500" 
		eternal="false"
		timeToIdleSeconds="300" 
		timeToLiveSeconds="1200" 
		overflowToDisk="true" />
	
	<cache 
		name="com.Liu.Pojo.User" 
		maxElementsInMemory="150" 
		eternal="false"
		timeToLiveSeconds="36000" 
		timeToIdleSeconds="3600" 
		overflowToDisk="true" />
</ehcache></span>
   三:修改要配置緩存的那個持久化類的對象關係映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.Liu.Pojo">
	<class name="User">
		<cache usage="read-only" region="com.Liu.Pojo.User" />
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="identity" />
		</id>
		<property name="uname" length="20" not-null="true" />
	</class>
</hibernate-mapping>

   四:在spring的配置文件中,hibernate部分加入xml 代碼

<!-- 啓用二級緩存,默認是(false)關閉的 --> 
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<!-- 設置查詢緩存 如果不設置'查詢緩存',那麼hibernate只會緩存使用load()方法獲得的單個持久化對象,
	如果想緩存使用 findall()、list()、Iterator()、createCriteria()、createQuery()等方法獲得的數據結果集的話,
	就需要設置 hibernate.cache.use_query_cache true 才行 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
 <!-- 指定緩存產品提供商 -->  
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
   五:在DAO中,調用find方法查詢之前,設置使用緩存

若是hibernateTemplate則爲:

String hql = "from User u ";
List<User> userList = new ArrayList<User>();
hibernateTemplate.setCacheQueries(true);
userList = hibernateTemplate.find(hql);

若是sessionFactory則爲:

List all = null;
String hql = "from User u";
Query q = this.getSession().createQuery(hql);
q.setCacheable(true);//</span>利用緩存
all = q.list();

   六:在頁面點擊查詢數據庫中的說有User表下所有的用戶名,並注意看console發出的查詢語句

         1.沒有設置緩存時:我點擊頁面的查詢按鈕,觀看頁面結果和後臺打印的信息,明顯的可以看到發出了3次查詢語句。

         

         2.設置緩存時:我點擊頁面的查詢按鈕,觀看頁面結果和後臺打印的信息,明顯的可以看到發出了1次查詢語句。

         

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章