Search code examples
spring-bootehcacheconfiguration-filesjcache

Ehcache jsr107:defaults not applying to programmatically created caches


Based on my findings in my previous SO question, I'm trying to setup JCaches in a mix of declarative and imperative configuration, to limit the max size of caches declaratively.

I keep a list of the caches and the duration (TTL) for their entries in my application.yaml, which I get with a property loader. I then create my caches with the code below:

@Bean
public List<javax.cache.Cache<Object, Object>> getCaches() {
    javax.cache.CacheManager cacheManager = this.getCacheManager();
    List<Cache<Object, Object>> caches = new ArrayList();
    Map<String, String> cacheconfigs = //I populate this with a list of cache names and durations;
    Set<String> keySet = cacheconfigs.keySet();
    Iterator i$ = keySet.iterator();

    while(i$.hasNext()) {
        String key = (String)i$.next();
        String durationMinutes = (String)cacheconfigs.get(key);
         caches.add((new GenericDefaultCacheConfigurator.GenericDefaultCacheConfig(key, new Duration(TimeUnit.MINUTES, Long.valueOf(durationMinutes)))).getCache(cacheManager));
    }

    return caches;
}

@Bean
public CacheManager getCacheManager() {
    return Caching.getCachingProvider().getCacheManager();
}

private class GenericDefaultCacheConfig {
    public GenericDefaultCacheConfig(String cacheName, Duration duration) {
        public GenericDefaultCacheConfig(String id, Duration duration, Factory expiryPolicyFactory) {
        CACHE_ID = id;
        DURATION = duration;
        EXPIRY_POLICY = expiryPolicyFactory;
    }
    private MutableConfiguration<Object, Object> getCacheConfiguration() {
        return new MutableConfiguration<Object, Object>()
                    .setTypes(Object.class, Object.class)
                    .setStoreByValue(true)
                    .setExpiryPolicyFactory(EXPIRY_POLICY);
    }
    public Cache<Object, Object> getCache(CacheManager cacheManager) {
        CacheManager cm = cacheManager;
        Cache<K, V> cache = cm.getCache(CACHE_ID, Object.class, Object.class);
        if (cache == null)
           cache = cm.createCache(CACHE_ID, getCacheConfiguration());
        return cache;
    }
}

I try limiting the cache size with the following ehcache.xml:

<config
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns='http://www.ehcache.org/v3'
    xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
    xsi:schemaLocation="
    http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
    http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

<service>
    <jsr107:defaults default-template="heap-cache" enable-management="true" enable-statistics="true">
    </jsr107:defaults>
</service>

<cache-template name="heap-cache">
    <resources>
        <heap unit="entries">20</heap>
    </resources>
</cache-template> </config>

I set the following declaration in my application.yaml:

spring:
  cache:
    jcache:
      config: classpath:ehcache.xml

However, my caches don't honor the imposed limit. I validate with the following test:

    @Test
public void testGetCacheMaxSize() { 
    Cache<Object, Object> cache = getCache(MY_CACHE); //I get a cache of type Eh107Cache[myCache]

    CacheRuntimeConfiguration<Object, Object> ehcacheConfig = (CacheRuntimeConfiguration<Object, Object>)cache.getConfiguration(
            Eh107Configuration.class).unwrap(CacheRuntimeConfiguration.class);
    long size = ehcacheConfig.getResourcePools().getPoolForResource(ResourceType.Core.HEAP).getSize(); //Returns 9223372036854775807 instead of the expected 20

    for(int i=0; i<30; i++)
        commonDataService.getAllStates("ENTRY_"+i);

    Map<Object, Object> cachedElements = cacheManagerService.getCachedElements(MY_CACHE);
    assertTrue(cachedElements.size().equals(20)); //size() returns 30
}

Can somebody point out what I am doing wrong? Thanks in advance.


Solution

  • The issue comes from getting the cache manager as:

    Caching.getCachingProvider().getCacheManager();
    

    By setting the config file URI on cache manager's initialization I got it to work:

    cachingProvider = Caching.getCachingProvider();
    configFileURI = resourceLoader.getResource(configFilePath).getURI();
    cacheManager = cachingProvider.getCacheManager(configFileURI, cachingProvider.getDefaultClassLoader());
    

    I was under the expectation that Spring Boot would automatically create the cache manager based on the configuration file included given in property spring.cache.jcache.config, but that was not the case because I get the cache manager as described above instead of simply auto-wiring it and letting Spring create it.