ehcache统计信息不正确:命中+未命中== 0

Joh*_*erg 13 java spring annotations hibernate ehcache

我有一个问题,net.sf.ehcache.CacheManager出现返回无效的统计信息.

我正在使用ehcache-core v2.3.2(最新版本)ehcache-spring-annotations.

问题是,getMemoryStoreObjectCount返回1个对象同时兼具getCacheHitsgetCacheMisses返回0.应该是总计数hits + misses吗?

下面的单元测试应该说明问题(它应用于数据库):

@Test
public void testCache() {
    Entity e = ..
    dao.storeEntity(e);
    dao.getEntity(e);
    assertEquals(1, cache.getStatistics().getMemoryStoreObjectCount()); // ok
    assertEquals(0, cache.getStatistics().getCacheHits()); // ok
    assertEquals(1, cache.getStatistics().getCacheMisses()); // fails due to 0

}
Run Code Online (Sandbox Code Playgroud)

为了完整性,我包括所有必要的配置:

Spring配置

<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

ehcache.xml中

<ehcache>
     <defaultCache eternal="false" maxElementsInMemory="1000"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>
</ehcache>
Run Code Online (Sandbox Code Playgroud)

@Cacheable(keyGenerator=@KeyGenerator(name="StringCacheKeyGenerator"))
public Entity getEntity(Serializable key) {
    return // sql ... 
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*erg 16

通过在以下位置设置以下属性找到问题的解决方案net.sf.ehcache.hibernate.EhCache:

  cache.setStatisticsEnabled(true);
  cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);
Run Code Online (Sandbox Code Playgroud)


aar*_*gas 16

statistics ="true"添加到您的ehcache.xml中,通常最好将配置更改保留在代码之外.

<ehcache>
     <defaultCache ... statistics="true" />
...
</ehcache>
Run Code Online (Sandbox Code Playgroud)


小智 5

<defaultCache ... statistics="true" />极大地与老办法cache.setStatisticsEnabled(真)对比的作品; 需要较低版本的ehcache(核心等).