需要帮助。 我正在开发一个项目,当它在一段时间内没有使用时我必须删除元素(键)。我尝试使用timeToLiveSeconds和timeToIdleSeconds作为60秒。还尝试了只有timeToLiveSeconds = 60并且只有timeToIdleSeconds = 60。但我仍然在缓存中看到该元素。
fyi:我有一些代码可以在屏幕上显示所有缓存的元素。
屏幕截图0:在为我的操作调用REST服务之前。期望:元素不应出现在缓存网页中 - 按预期工作
屏幕截图1:调用REST服务后执行我的操作。期望:请参阅网页中缓存列表中的元素 - 按预期工作
现在空闲60秒(不要调用REST服务,意味着元素不使用60秒)
屏幕截图2:刷新缓存网页。期望:元素不应出现在缓存网页中 - 不按预期工作
这是我的代码:
/**
*
* @param cacheName
* @param diskPersistent
*/
private static void addCache(String cacheName, boolean diskPersistent) {
/*
@param name - the name of the cache. Note that "default" is a reserved name for the defaultCache.
@param maxElementsInMemory - the maximum number of elements in memory, before they are evicted (0 == no limit)
@param memoryStoreEvictionPolicy - one of LRU, LFU and FIFO. Optionally null, in which case it will be set to LRU.
@param overflowToDisk - whether to use the disk store
@param diskStorePath - this parameter is ignored. CacheManager sets it using setter injection.
@param eternal - whether the elements in the cache are eternal, i.e. never expire
@param timeToLiveSeconds - the default amount of time to live for an element from its creation date
@param timeToIdleSeconds - the default amount of time to live for an element from its last accessed or modified date
@param diskPersistent - whether to persist the cache to disk between JVM restarts
@param diskExpiryThreadIntervalSeconds - how often to run the disk store expiry thread. A large number of 120 seconds plus is @param recommended
@param registeredEventListeners - a notification service. Optionally null, in which case a new one with no registered listeners will @param be created.
@param bootstrapCacheLoader - the BootstrapCacheLoader to use to populate the cache when it is first initialised. Null if none is @param required.
@param maxElementsOnDisk - the maximum number of Elements to allow on the disk. 0 means unlimited.
@param diskSpoolBufferSizeMB - the amount of memory to allocate the write buffer for puts to the DiskStore.
*/
Cache cache = new Cache(cacheName,
10000, // maxElementsInMemory
MemoryStoreEvictionPolicy.LRU, // memoryStoreEvictionPolicy
false, // overflowToDisk
diskStorePath,
true, // eternal
60, // timeToLiveSeconds
60, // timeToIdleSeconds
false, // diskPersistent
1, // diskExpiryThreadIntervalSeconds
null,
null,
0, // bootstrapCacheLoader
0, // maxElementsOnDisk
false);
cacheManager.addCache(cache);
}
答案 0 :(得分:2)
请注意,当永恒属性设置为“ true”时,它将覆盖timeToLive和timeToIdle,因此不会发生过期。 (https://www.ehcache.org/documentation/2.8/configuration/configuration.html) 在您的配置中,参数“永恒”具有“真实”价值。
答案 1 :(得分:0)
Ehcache只会在访问或驱逐时删除过期的元素。如果你根本不与缓存进行交互,那么就不会出现急切的过期 - 就像后台线程一样 -
这意味着:
cache.put(new Element(key, value)); // Assume cache TTL is 60 seconds
Thread.sleep(30000);
assert cache.getSize() == 1; // as there is one mapping
cache.get(key); // Returns the mapping as it is not expired
Thread.sleep(40000); // No interactions with the cache, the entry is still in there.
cache.put(new Element(otherKey, otherValue)); // Would remove the other entry if max size is 1
assert cache.getSize() == 2; // assuming the cache can hold more than one mapping
cache.get(key); // Would return null because the value is expired and thus removed upon access.
assert cache.getSize() == 1; // because the previous get did finally remove the expired mapping.