ehcache jsr107:默认值不适用于以编程方式创建的缓存

时间:2018-10-12 16:22:55

标签: spring-boot ehcache configuration-files jcache

根据我之前的SO question的发现,我试图以声明性和命令性配置的混合设置JCache,以声明性地限制缓存的最大大小。

我在我的application.yaml中保留了缓存列表及其条目的持续时间(TTL),该列表是通过属性加载器获得的。然后,使用以下代码创建缓存:

@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;
    }
}

我尝试使用以下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>

我在application.yaml中设置了以下声明:

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

但是,我的缓存不符合所施加的限制。我通过以下测试进行验证:

    @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
}

有人可以指出我做错了什么吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

问题来自将缓存管理器设置为:

Caching.getCachingProvider().getCacheManager();

通过在缓存管理器的初始化中设置配置文件URI,我可以使其正常工作:

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

我期望Spring Boot将基于属性 spring.cache.jcache.config 中提供的配置文件自动创建缓存管理器, 但是事实并非如此,因为我得到了如上所述的缓存管理器,而不是简单地自动装配并让Spring创建它。