0

在这里需要帮助。我正在做一个项目,当它在一段时间内不使用时,我必须删除元素(键)。我尝试将 timeToLiveSeconds 和 timeToIdleSeconds 都设置为 60 秒。还尝试仅使用 timeToLiveSeconds=60 和仅使用 timeToIdleSeconds=60。但我仍然在缓存中看到该元素。

仅供参考:我有一些代码可以在屏幕上显示所有缓存的元素。

屏幕截图 0:在为我的操作调用 REST 服务之前。期望:元素不应该出现在缓存网页中——按预期工作

屏幕截图 1:调用 REST 服务执行我的操作后。预期:查看网页缓存列表中的元素——按预期工作

现在空闲 60 秒(不调用 REST 服务,表示该元素 60 秒未使用)

屏幕截图 2:刷新缓存网页。预期:该元素不应出现在缓存网页中 - 未按预期工作

  • 元素表示 AF2TDU2001
  • 我们正在使用 ehcache 2.5.2

截图 0

截图 1

截图 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);
    }
4

2 回答 2

2

请注意,当设置为“true”时,永恒属性会覆盖 timeToLive 和 timeToIdle,因此不会发生过期。(https://www.ehcache.org/documentation/2.8/configuration/configuration.html)并且在您的配置参数中,“永恒”具有“真实”值。

于 2019-08-08T11:23:03.253 回答
1

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.
于 2018-03-13T11:29:10.843 回答