Ehcache Element Cache Life

The image creating the Spring service is as follows:

  + -------- + + --------- + + --------- +
 | Backend |  --------- |  My |  ----------- |  Clients |
 | service |  |  service |  |  |
 + -------- + + --------- + + --------- +

In order not to make too many requests for the backend, I use Ehcache. Imagine that my service is a sports portal, and I cache the results so that when the client requests them, I return the cache, if there is no cache, I take information from the backend, put it in the cache, and then return it to the client.

Now, if I have these options, set

long timeToLiveSeconds long timeToIdleSeconds 

and clients make requests too often, timeToIdleSeconds will not expire, but timeToLiveSeconds will expire anyway, and the item will be deleted from the cache in which I put it, right?

+4
source share
2 answers

Yes you are right.

For instance:

timeToIdleSeconds = 30 : if the cached object was not requested within these 30 seconds, it expires => last access time counter

timeToLiveSeconds = 60 : after 60 seconds, the cached object expires - regardless of how often it was requested or requested at all => creation time

From the docs (earlier version 1.4):

timeToIdleSeconds : Sets the timeout for an item before its expiration. that is, the maximum period of time between access until the item expires. Used only if the item is not eternal. Optional attribute. A value of 0 means that the element can stand idle indefinitely. The default value is 0.

timeToLiveSeconds : Sets the time for an item to live before it expires. that is, the maximum time between the creation time and the expiration of an item. It is used only if the element is not eternal. Optional attribute. A value of 0 means that the Element can live indefinitely. The default value is 0.

+4
source

If you set both parameters, expirationTime will be Math.min(ttlExpiry, ttiExpiry) , where

 ttlExpiry = creationTime + timeToLive ttiExpiry = mostRecentTime + timeToIdle 

Full source code is here .

0
source

Source: https://habr.com/ru/post/1380591/


All Articles