Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect ehcache statistics: hits+misses == 0

I have a problem where net.sf.ehcache.CacheManager appears returns invalid statistics.

I'm using ehcache-core v2.3.2 (latest version) with ehcache-spring-annotations.

The problem is that getMemoryStoreObjectCount returns 1 object while both getCacheHits and getCacheMisses returns 0. Isn't the total count supposed to be hits + misses ?

The unit test below should illustrate the problem (it's applied to an empty database):

@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

}

For completeness I include all essential configuration:

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>

dao

@Cacheable(keyGenerator=@KeyGenerator(name="StringCacheKeyGenerator"))
public Entity getEntity(Serializable key) {
    return // sql ... 
}
like image 751
Johan Sjöberg Avatar asked Mar 10 '11 18:03

Johan Sjöberg


2 Answers

Add statistics="true" to your ehcache.xml, it's usually better to keep your configuration changes outside your code.

<ehcache>
     <defaultCache ... statistics="true" />
...
</ehcache>
like image 199
aaronvargas Avatar answered Nov 04 '22 20:11

aaronvargas


Found the solution to the problem by setting the following properties in net.sf.ehcache.hibernate.EhCache:

  cache.setStatisticsEnabled(true);
  cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);
like image 16
Johan Sjöberg Avatar answered Nov 04 '22 21:11

Johan Sjöberg