有没有办法修改以声明方式声明的 JCache 的过期策略?

Is there a way to modify a JCache's expiry policy that has been declared declaratively?

我正在使用 Hazelcast 3.12 jCache 实现。我的缓存已声明声明(通过 hazelcast-config.xml),但是我希望能够动态更改 ExpiryPolicy 的持续时间,因为超时值只能通过编程方式获得。

我查看了 ICache 的文档,但没有看到任何方法可以让我检索 and/or 修改整个缓存的过期策略。

我的缓存声明为:

<cache name="toto">
    <async-backup-count>1</async-backup-count>
    <backup-count>1</backup-count>
    <cache-entry-listeners>
        <cache-entry-listener old-value-required="true">
            <cache-entry-listener-factory class-name="cache.UserRolesEntryListenerStaticFactory"/>
        </cache-entry-listener>
    </cache-entry-listeners>
    <key-type class-name="java.lang.String"/>
    <management-enabled>true</management-enabled>
    <statistics-enabled>true</statistics-enabled>
    <quorum-ref>1</quorum-ref>
    <partition-lost-listeners></partition-lost-listeners>
</cache>

我想 set/update 检索时的过期政策为:

@Produces
@Singleton
@UserRolesCache
public Cache<String, String[]> createUserRoleCache(@HazelcastDistributed CacheManager cacheManager) {
    Cache cache = cacheManager.getCache("toto");

    // get expiry timeout from a 3rd service
    int timeout = configService.getCacheExpiry();

    // how to set the expiry policy here???
    // cache.setExpiryPolicy(.....) ?????
}

使用 jCache 或 Hazelcast API 这可行吗?

没有明确的方法来更改 Cache 的默认过期策略(在 JCache 或 Hazelcast 特定 APIs).

您可以通过使用自定义 Factory<ExpiryPolicy> 配置 Cache 来达到相同的效果。 在您的 ExpiryPolicy 实施中,您可以咨询您的外部服务以查询当前 到期持续时间和 return 以便 JCache 实现应用它。请注意,由于过期政策 在每个条目creation/access/update上查询,最好ExpiryPolicy方法实现不涉及 任何远程服务查询或数据库访问,否则您将遇到高延迟。例如, 最好将您的过期政策注册为外部服务的侦听器(如果支持) 或者有一个单独的执行程序来安排对外部服务的更新查询。

使用 JCache 的示例实现 API:

class Scratch {
    public static void main(String[] args)
            throws InterruptedException {
        MutableConfiguration<String, String[]> roleCacheConfig = new MutableConfiguration<>();
        // other config here...
        roleCacheConfig.setExpiryPolicyFactory(new CustomExpiryPolicyFactory());

        CachingProvider provider = Caching.getCachingProvider();
        CacheManager manager = provider.getCacheManager();
        Cache<String, String[]> cache = manager.createCache("test", roleCacheConfig);

        cache.put("a", new String[]{}); // consults getExpiryForCreation
        Thread.sleep(1000);
        cache.get("a"); // consults getExpiryForAccess
        Thread.sleep(1000);
        cache.put("a", new String[]{}); // consults getExpiryForUpdate
    }

    public static class CustomExpiryPolicyFactory implements Factory<ExpiryPolicy>, Serializable {
        @Override
        public ExpiryPolicy create() {
            // initialize custom expiry policy: at this point
            // the custom expiry policy should be registered as listener to
            // external service publishing expiry info or otherwise configured
            // to poll the external service for new expiry info
            CustomExpiryPolicy expiryPolicy = new CustomExpiryPolicy(120, 30, 120);
            return expiryPolicy;
        }
    }

    public static class CustomExpiryPolicy implements ExpiryPolicy {

        private volatile Duration expiryForCreation;
        private volatile Duration expiryForAccess;
        private volatile Duration expiryForUpdate;

        public CustomExpiryPolicy(long secondsForCreation,
                           long secondsForAccess,
                           long secondsForUpdate) {
            this.expiryForCreation = new Duration(TimeUnit.SECONDS, secondsForCreation);
            this.expiryForAccess = new Duration(TimeUnit.SECONDS, secondsForAccess);
            this.expiryForUpdate = new Duration(TimeUnit.SECONDS, secondsForUpdate);
        }

        // assuming this is invoked from external service whenever there is a change
        public void onExpiryChanged(long secondsForCreation,
                                    long secondsForAccess,
                                    long secondsForUpdate) {
            this.expiryForCreation = new Duration(TimeUnit.SECONDS, secondsForCreation);
            this.expiryForAccess = new Duration(TimeUnit.SECONDS, secondsForAccess);
            this.expiryForUpdate = new Duration(TimeUnit.SECONDS, secondsForUpdate);
        }

        @Override
        public Duration getExpiryForCreation() {
            return expiryForCreation;
        }

        @Override
        public Duration getExpiryForAccess() {
            return expiryForAccess;
        }

        @Override
        public Duration getExpiryForUpdate() {
            return expiryForUpdate;
        }
    }
}

您可以在声明式 Hazelcast XML 配置中提供您的自定义到期策略工厂 class 名称,如下所示:

<cache name="toto">
    <async-backup-count>1</async-backup-count>
    <backup-count>1</backup-count>
    ...
    <expiry-policy-factory class-name="com.example.cache.CustomExpirePolicyFactory" />
    ...
</cache>

作为旁注,ICache 中有一些方法,Hazelcast 特定的扩展 Cache 接口,允许 您使用为每个键指定的自定义过期策略对一个或一组键执行操作(但不更改缓存范围内适用的过期策略)。