2012-05-15 137 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(真)”因为这是负责上市的方法/加载实体的查询没有得到缓存。