日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
精通Hibernate管理緩存

無論何時,我們在管理Hibernate緩存(Managing the caches)時,當你給save()、update()或saveOrUpdate()方法傳遞一個對象時,或使用load()、 get()、list()、iterate() 或scroll()方法獲得一個對象時, 該對象都將被加入到Session的內(nèi)部緩存中。

當隨后flush()方法被調(diào)用時,對象的狀態(tài)會和數(shù)據(jù)庫取得同步。 如果你不希望此同步操作發(fā)生,或者你正處理大量對象、需要對有效管理內(nèi)存時,你可以調(diào)用evict() 方法,從一級緩存中去掉這些對象及其集合。

 
 
 
  1. ScrollableResult cats = sess.createQuery("from Cat as cat").scroll(); //a huge result set  
  2. while ( cats.next() ) {  
  3.     Cat cat = (Cat) cats.get(0);  
  4.     doSomethingWithACat(cat);  
  5.     sess.evict(cat);  

Session還提供了一個contains()方法,用來判斷某個實例是否處于當前session的緩存中。

如若要把所有的對象從session緩存中徹底清除,則需要調(diào)用Session.clear()。

對于二級緩存來說,在SessionFactory中定義了許多方法, 清除緩存中實例、整個類、集合實例或者整個集合。

 
 
 
  1. sessionFactory.evict(Cat.class, catId); //evict a particular Cat  
  2. sessionFactory.evict(Cat.class);  //evict all Cats  
  3. sessionFactory.evictCollection("Cat.kittens", catId); //evict a particular collection of kittens  
  4. sessionFactory.evictCollection("Cat.kittens"); //evict all kitten collections 

CacheMode參數(shù)用于控制具體的Session如何與二級緩存進行交互。

CacheMode.NORMAL - 從二級緩存中讀、寫數(shù)據(jù)。

CacheMode.GET - 從二級緩存中讀取數(shù)據(jù),僅在數(shù)據(jù)更新時對二級緩存寫數(shù)據(jù)。

CacheMode.PUT - 僅向二級緩存寫數(shù)據(jù),但不從二級緩存中讀數(shù)據(jù)。

CacheMode.REFRESH - 僅向二級緩存寫數(shù)據(jù),但不從二級緩存中讀數(shù)據(jù)。通過 hibernate.cache.use_minimal_puts的設置,強制二級緩存從數(shù)據(jù)庫中讀取數(shù)據(jù),刷新Hibernate緩存內(nèi)容。

如若需要查看二級緩存或查詢Hibernate緩存區(qū)域的內(nèi)容,你可以使用統(tǒng)計(Statistics) API。

 
 
 
  1. Map cacheEntries = sessionFactory.getStatistics()  
  2.         .getSecondLevelCacheStatistics(regionName)  
  3.         .getEntries(); 

此時,你必須手工打開統(tǒng)計選項。可選的,你可以讓Hibernate更人工可讀的方式維護Hibernate緩存內(nèi)容。

 
 
 
  1. hibernate.generate_statistics true  
  2. hibernate.cache.use_structured_entries true 

【編輯推薦】

  1. 簡述Hibernate中加載并存儲對象
  2. Hibernate傳播性持久化攻略
  3. 深入了解Hibernate自動狀態(tài)檢測
  4. 簡單學會Hibernate對象持久化
  5. 分析Hibernate自增主鍵

當前標題:精通Hibernate管理緩存
轉(zhuǎn)載源于:http://www.5511xx.com/article/dhidjpd.html