2012-05-15 131 views
4

我有這個春季/休眠項目,我試圖通過ehcache和兵馬俑將第二級緩存添加到休眠狀態。一切看起來都很好,我甚至可以在terracota控制檯中看到我試圖緩存的實體條目。但基於數據庫的統計和日誌,根本沒有任何緩存!通過ehcache與兵馬俑休眠二級緩存 - 根本不緩存?

負載命中率爲0%,負載統計量也爲0。那我做錯了什麼?

這是我做的,我通過maven添加了需要的罐子。

 <dependency> 
       <groupId>net.sf.ehcache</groupId> 
       <artifactId>ehcache-core</artifactId> 
       <version>2.5.2</version> 
     </dependency> 
     <dependency> 
       <groupId>net.sf.ehcache</groupId> 
       <artifactId>ehcache-terracotta</artifactId> 
       <version>2.5.2</version> 
     </dependency> 
     <dependency> 
       <groupId>org.terracotta</groupId> 
       <artifactId>terracotta-toolkit-1.5-runtime</artifactId> 
       <version>4.2.0</version> 
     </dependency> 

改變了我的休眠特性,使二級高速緩存

<property name="hibernateProperties"> 
      <props> 
       ... 
       <prop key="hibernate.cache.use_second_level_cache">true</prop> 
       <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop> 
       <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop> 
       <prop key="hibernate.cache.use_query_cache">true</prop> 
       <prop key="hibernate.cache.use_structured_entries">true</prop> 
       <prop key="hibernate.cache.generate_statistics">true</prop> 
      </props> 
     </property> 

添加了@Cache註釋到我的測試實體

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) 
public class User implements java.io.Serializable 
{ 
... 
} 

這是我極其簡單ehcache.xml中(我也試着爲我的實體設置一個緩存條目,結果相同)

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache >  
    <defaultCache 
     maxElementsInMemory="10000" 
     eternal="false" 
     maxEntriesLocalHeap="10"   
     timeToIdleSeconds="120"   
     timeToLiveSeconds="120">   
     <terracotta/>  
    </defaultCache>   
    <terracottaConfig   
     url="localhost:9510"/> 
</ehcache> 

我開始我的兵馬俑服務器並運行我的測試代碼

@Test 
    @Transactional 
    @Rollback(false) 
    public void testCache() { 
     long start = System.currentTimeMillis(); 
     List<User> list = userRepository.listAll(0, 100); 
     long end = System.currentTimeMillis(); 
     log.info("Total time "+(end-start)); 
     assertNotNull(list); 
     assertThat(list.size(), is(100)); 

     for (int i=0; i<100; i++) { 
      long start2 = System.currentTimeMillis(); 
      list = userRepository.listAll(0, 100); 
      long end2 = System.currentTimeMillis(); 
      log.info("Total time 2 "+(end2-start2)); 
     } 
     assertNotNull(list); 
     assertThat(list.size(), is(100)); 
    } 

後我的日誌顯示不應該發生100 SQL。它也顯示命中率爲0%。

下面是我的測試運行時陶土控制檯的一些屏幕截圖。

這是我需要的最後一件東西,爲了使這個工作?

2nd level cache statistics ehcache overview Entity statistics

+0

您使用的@Cache註釋是在Spring框架中提供的還是其他的? –

+0

我看到你已經啓用了Hibernate統計數據......我認爲第二個屏幕截圖實際上從那裏獲取統計信息,但是你可以在代碼中驗證嗎?如果這些值是正確的,出於某種原因,Hibernate根本不使用二級緩存......你可以通過id加載實體嗎?其實訪問列表中的東西? –

+0

另外,由於測試實際上是在測試延遲,所以您最好關閉hibernate.cache.use_structured_entries。除非你真的計劃檢查緩存中的東西... –

回答

3

找到了解決我嘗試的問題,下面是詳細信息:

  • 的統計數據並沒有獲得應有設置爲休眠特性錯鍵

使用此項(注意號碼.cache。)的

<prop key="hibernate.generate_statistics">true</prop> 

代替

<prop key="hibernate.cache.generate_statistics">true</prop> 
  • ,因爲我沒有使用「.setCachable(真)」因爲這是負責上市的方法/加載實體的查詢沒有得到緩存。