How to change a EhCache members expiry time programmatically

倾然丶 夕夏残阳落幕 提交于 2019-12-07 12:51:39

问题


I would like to change the expiry or set the expiry time of a member of an EhCache via Java code.

I know when the object should expire, but I'm unsure how to achieve this.

I know I can set it for the whole cache, e.g.

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

Can someone suggest how I do this for a specific member?


回答1:


In Ehcache 2.x, you can set expiry time on the Element you insert in the cache:

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

In Ehcache 3.x, you can implement a custom Expiry and have it return different Duration depending on the key and value:

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

Check the API documentation for more information.



来源:https://stackoverflow.com/questions/37697751/how-to-change-a-ehcache-members-expiry-time-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!