简体   繁体   中英

How to enable ehcache statistics on spring xml file

Based on following configuration I cannot enable ehcache statistics on xml file. There is no such a property to enable ehcache statistics.

<bean id="cache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  <property name="cacheName" value="diskCache"/>
  <property name="cacheManager" ref="cacheManager"/>
  <property name="maxElementsInMemory" value="1"/>
  <property name="overflowToDisk" value="true"/>
  <property name="maxElementsOnDisk" value="10"/>
</bean>

Ehcache 2.4.6 comes with disabled cache statistics.

Has anyone else had experience implementing this?

Any help or ideas on this would be MUCH appreciated!

Looks like the built-in EhCacheFactoryBean does not support setting this flag (I encourage you to open a feature request ). However it is relatively easy to add this by yourself:

package com.example;

public class EhCacheWithStatisticsFactoryBean extends EhCacheFactoryBean {

    private boolean statisticsEnabled;

    @Override
    public void afterPropertiesSet() throws CacheException, IOException {
        super.afterPropertiesSet();
        getObject().setStatisticsEnabled(statisticsEnabled);
    }

    public void setStatisticsEnabled(boolean statisticsEnabled) {
        this.statisticsEnabled = statisticsEnabled;
    }
}

And usage:

<bean id="cache" class="com.example.EhCacheWithStatisticsFactoryBean">
  <property name="cacheName" value="diskCache"/>
  <property name="cacheManager" ref="cacheManager"/>
  <property name="maxElementsInMemory" value="1"/>
  <property name="overflowToDisk" value="true"/>
  <property name="maxElementsOnDisk" value="10"/>
  <property name="statisticsEnabled" value="true"/> <!-- HERE -->
</bean>

Of course the easy path would be to use standard ehcache.xml file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM