我想通过Java代码更改EhCache成员的到期时间或设置到期时间。
我知道该对象何时应该过期,但我不确定如何实现这一点。
我知道我可以为整个缓存设置它,例如
Cache cache = manager.getCache("sampleCache");
CacheConfiguration config = cache.getCacheConfiguration();
config.setTimeToIdleSeconds(60);
config.setTimeToLiveSeconds(120);
config.setMaxEntriesLocalHeap(10000);
config.setMaxEntriesLocalDisk(1000000);
有人可以建议我如何为特定成员执行此操作吗?
在Ehcache 2.x中,可以设置过期时间Element
您在缓存中插入:
Element element = new Element("key1", "value1");
element.setTimeToLive(300);
在Ehcache 3.x中,你可以实现自定义Expiry
并让它返回不同的Duration
取决于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);
}
检查API文档 http://www.ehcache.org/apidocs/3.0.2/index.html了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)