【问题标题】:How to change a EhCache members expiry time programmatically如何以编程方式更改 EhCache 成员的到期时间
【发布时间】:2016-06-08 08:55:42
【问题描述】:

我想通过 Java 代码更改 EhCache 成员的到期时间或设置到期时间。

我知道对象应该何时过期,但我不确定如何实现。

我知道我可以为整个缓存设置它,例如

Cache cache = manager.getCache("sampleCache");
CacheConfiguration config = cache.getCacheConfiguration();
config.setTimeToIdleSeconds(60);
config.setTimeToLiveSeconds(120);
config.setMaxEntriesLocalHeap(10000);
config.setMaxEntriesLocalDisk(1000000);

有人可以建议我如何为特定成员执行此操作吗?

【问题讨论】:

  • 如果您能说出您想要达到的目标,这可能会有所帮助。你能提供一个用例吗?

标签: java ehcache


【解决方案1】:

在 Ehcache 2.x 中,您可以在缓存中插入的 Element 上设置过期时间:

Element element = new Element("key1", "value1");
element.setTimeToLive(300);

在 Ehcache 3.x 中,您可以实现自定义 Expiry 并让它根据 keyvalue 返回不同的 Duration

public interface Expiry<K, V> {
  Duration getExpiryForCreation(K key, V value);
  Duration getExpiryForAccess(K key, ValueSupplier<? extends V> value);
  Duration getExpiryForUpdate(K key, ValueSupplier<? extends V> oldValue, V newValue);
}

查看API documentation 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多