简体   繁体   English

如何使用多个到期策略配置Apache Ignite缓存

[英]How to configure Apache Ignite cache with multiple expiry policies

For example, I would like to configure the cache with the following two expiry policies: 例如,我要使用以下两个到期策略配置缓存:

  • TouchedExpiryPolicy TouchedExpiryPolicy
  • CreatedExpiryPolicy CreatedExpiryPolicy

The sample code is as below (Apache Ignite version 1.5.0.final): 示例代码如下(Apache Ignite版本1.5.0.final):

public IgniteCache<String, Object> getOrCreateCache(String cacheName) {
    Ignite ignite = Ignition.ignite();

    CacheConfiguration<String, Object> cacheCfg = new CacheConfiguration<String, Object>(cacheName);
    cacheCfg.setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 10)));
    cacheCfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 30)));

    IgniteCache<String, Object> igniteCache = ignite.getOrCreateCache(cacheCfg);
    return igniteCache;     
}

The problem is, however, that the second expiry policy will replace the first one. 问题是,第二个过期策略将取代第一个过期策略。 Wonder if there is any way I can configure the Ignite cache so that the cache will honour both expiry policies? 想知道是否有什么方法可以配置Ignite缓存,以便该缓存可以同时满足两个到期策略? Thank you. 谢谢。

By the way, in EhCache, I can achieve the same thing by configuring the cache the following way: 顺便说一下,在EhCache中,我可以通过以下方式配置缓存来实现相同的目的:

<cache name="my-cache-name" ...
    timeToIdleSeconds="10" timeToLiveSeconds="30"
    ...>        
</cache>

References: 参考文献:

Answering for general JCache ExpiryPolicy, maybe there are additional options in Apache Ignite. 回答一般的JCache ExpiryPolicy,也许Apache Ignite中还有其他选项。

The TouchedExpiryPolicy uses the same duration for creation and update. TouchedExpiryPolicy使用相同的持续时间进行创建和更新。

You can set individual times, by subclassing the ExpiryPolicy . 您可以通过将ExpiryPolicy子类化来设置单个时间。

Be careful about the logic implications. 注意逻辑含义。 Setting 10 seconds expiry after access and 30 seconds after creation means for example: 设置访问后10秒过期和创建后30秒设置为例如:

  • Item is created, stays 30 seconds in the cache, if not accessed 项目已创建,如果未访问,则在缓存中保留30秒
  • Item is created, is accessed 5 seconds after creation, item expires 15 seconds after creation (10 seconds after access) 项目已创建,在创建后5秒钟被访问,项目在创建后15秒钟到期(访问后10秒钟)

Probably you want to achieve something different. 可能您想实现一些不同的目标。 So the answer is: Mixing TTL and TTI isn't possible the way it is designed. 因此,答案是:不能将TTL和TTI混合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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