0

Need a help here. I am working on a project where I have to delete elements(keys) when it is not used for a certain time. I tried with both timeToLiveSeconds and timeToIdleSeconds as 60 seconds. Also tried with only timeToLiveSeconds=60 and with only timeToIdleSeconds=60. But I still see that element in cache.

fyi: I have some code to show all cached elements on the screen.

Screenshot 0: Before calling REST service for my action. Expectation: the element Should NOT appear in the Cache Web page – Working as expected

Screenshot 1: After calling REST service to execute my action. Expectation: See the element in the Cache list in the web page – Working as expected

Now Idle for 60 seconds (do not call the REST service, means the element is not used for 60 seconds)

Screenshot 2: Refresh the Cache Web page. Expectation: the element Should NOT appear in the Cache Web page – NOT working as expected

  • element means AF2TDU2001
  • We are using ehcache 2.5.2

Screenshot 0

Screenshot 1

Screenshot 2

Here is is my code:

/**
     * 
     * @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);
    }
SK.
  • 1,390
  • 2
  • 28
  • 59

2 Answers2

2

Note that the eternal attribute, when set to “true”, overrides timeToLive and timeToIdle so that no expiration can take place. (https://www.ehcache.org/documentation/2.8/configuration/configuration.html) And in your configuration parametr 'eternal' has 'true' value.

1

Ehcache will only remove expired elements on access or through eviction. If you do not interact with the cache at all, no eager expiry - like with a background thread - will happen.

That means:

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.
Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
  • 1
    Thanks for your time in helping me. When you are saying "only remove expired elements on access", does that mean if an element is not used for long time. I mean what does "expired elements" mean? Could you please elaborate a little more. – SK. Mar 14 '18 at 12:41