混合声明式和命令式JCache配置 [英] Mixing declarative and imperative JCache configurations

查看:182
本文介绍了混合声明式和命令式JCache配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以声明性和命令性配置的方式设置(J)缓存,因为JCache标准没有提供限制缓存可占用的最大大小的方法。我想尽可能地做到独立于提供商,因此将来我可以灵活地更改提供商。我相信,目前我有使用Java 7的限制,所以咖啡因被丢弃了。

I'm trying to setup (J)caches in a mix of declarative and imperative configuration, as the JCache standard doesn't provide a means to limit the max size a cache can occupy. I want to do it as much "provider independent" as possible, so I have the flexibility to change provider in the future. Currently I have the limitation of using Java 7, so Caffeine is discarded, I believe.

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

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<Cache<Object, Object>> getCaches() {
    CacheManager cacheManager = this.getCacheManager();
    List<Cache<Object, Object>> caches = new ArrayList();
    Map<String, String> cacheconfigs = this.cacheConfigPropertyLoader.getPropertyLoader().getCacheconfigs();
    Set<String> keySet = cacheconfigs.keySet();
    Iterator i$ = keySet.iterator();

    while(i$.hasNext()) {
        String name = (String)i$.next();
        String durationMinutes = (String)cacheconfigs.get(name);
        caches.add((new GenericDefaultCacheConfigurator.GenericDefaultCacheConfig(name, 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;
    }
}

对于创建我的缓存并将其与注释和命令性代码,无论我在POM中使用了哪个JCache提供程序(我都使用org.jsr107.ri,hazelcast和EhCache进行了测试)。

That works fine for creating my caches and using it with annotations and imperative code, no matter which JCache provider I use in the POM (I tested it with org.jsr107.ri, hazelcast, and EhCache).

现在我需要使用专有配置限制所有缓存的最大大小。我需要一个通用/默认配置,该配置适用于该提供程序创建的任何缓存,而与强制性配置设置的其他特定特征(到期策略,生存时间等)无关。

Now I need to limit the max size of all the caches with a proprietary configuration. I need a common/default configuration that would apply to any cache created by that provider, irrespective of their other particular characteristics (Expiry policy, time to live, etc.) which are set by the imperative configuration.

在包含配置文件时,使用配置文件配置用于创建在yaml文件中声明的这些缓存的缓存管理器时遇到了问题。有什么想法/建议吗?我记得曾经读过在Ehcache配置中使用*的地方,但是我再也找不到该页面。

I've been having issues, when including a configuration file, for the cache manager configured with the config file to be used to create these caches declared in my yaml file. Any ideas/suggestions? I recall reading somewhere of using * in Ehcache configuration, but I could not find that page again.

推荐答案

我将我的发现放在这里,以便将其用作参考。

I will put my findings here so they can be used as a reference.

Hazelcast

正如mdogan回答的那样,Hazelcast不支持此功能。它的概念是带通配符的配置(请检查此答复),但是这些不适用于以编程方式配置的缓存。

As mdogan replied, Hazelcast doesn't support this. It has the concept of configurations with wildcards (check this reply), but those don't apply to caches configured programmatically.

Ehcache

在Ehcache中,我找到了一种方法。根据其文档

In Ehcache I found a way. As per their documentation:


配置一个默认模板,所有以编程方式创建的Cache实例都将从该模板继承

Configure a default template from which all programmatically created Cache instances inherit

您需要声明一个默认模板,如下所示:

You need to declare a default-template as below:

<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="defCache"> 
    </jsr107:defaults>
  </service>

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

并在该缓存中设置所需的所有配置。这种声明性配置补充或覆盖了程序化配置。有关如何指定Ehcache的最大大小的指示,请参见这里

and set all configuration you like in that cache. This declarative configuration complements or even overrides the programmatic one. Indications of how to specify the max size of a Ehcache can be found here.

这篇关于混合声明式和命令式JCache配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆