If my Ehcache is configured using TTL, do I need to check if the cured item expires?

I cannot find it anywhere in Ehcache docs.

I am now using this code to create and configure my Cache:

// Groovy syntax

def cacheConfig = new CacheConfiguration('stats', 1)
cacheConfig.timeToLiveSeconds = 2

def cache = new Cache(cacheConfig)
cache.initialise()

and to obtain data:

// Groovy syntax

def cachedElement = cache.get('stats')

if (cachedElement != null && ! cachedElement.isExpired()) {
    // use the cached data
} else {
    // get/generate the data and cache it
}

return cachedElement.value

I wrote this a while ago, but looking at it now, it seems that it was stupid to check Element.isExpired()- it is not necessary, is it? I mean, if an item has expired, then the cache should not return it - right?

So, I think I can remove this check - I just hope that a quick check will be done here.

Thank!

+3
source share
2 answers

, . , . , javadoc , , -, .

+3

, cacheElement , . null, - , . Ecache , - , . config :

<ehcache:config cache-manager="ehCacheManager">
  <!-- interval is in minutes -->
  <ehcache:evict-expired-elements interval="20"/>
</ehcache:config>

, . , , isExpired().

0

All Articles