Подтвердить что ты не робот

Неверная статистика ehcache: hits + misses == 0

У меня проблема, когда net.sf.ehcache.CacheManager появляется, возвращает недопустимую статистику.

Я использую ehcache-core v2.3.2 (последняя версия) с ehcache-spring-annotations.

Проблема заключается в том, что getMemoryStoreObjectCount возвращает 1 объект, в то время как getCacheHits и getCacheMisses возвращает 0. Разве общий счет не должен быть hits + misses?

Ниже приведен пример unit test, который должен отображаться в пустой базе данных:

@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

}

Для полноты я включаю всю необходимую конфигурацию:

Spring config

<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
</bean>

ehcache.xml

<ehcache>
     <defaultCache eternal="false" maxElementsInMemory="1000"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>
</ehcache>

дао

@Cacheable([email protected](name="StringCacheKeyGenerator"))
public Entity getEntity(Serializable key) {
    return // sql ... 
}
4b9b3361

Ответ 1

Нашел решение проблемы, установив в net.sf.ehcache.hibernate.EhCache следующие свойства:

  cache.setStatisticsEnabled(true);
  cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);

Ответ 2

Добавьте в свой ehcache.xml statistics = "true" , обычно лучше сохранить изменения конфигурации вне вашего кода.

<ehcache>
     <defaultCache ... statistics="true" />
...
</ehcache>

Ответ 3

<defaultCache ... statistics="true" /> работает очень в отличие от старого способа cache.setStatisticsEnabled(true); который нуждается в более низкой версии ehcache (ядро и т.д.).