emilly
emilly

Reputation: 10530

Role of eternal and maxElementsInMemory in ehcache?

I have below configuration in my project

<defaultCache maxElementsInMemory="100" eternal="false" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" statistics="false" />

<cache name="customerList" maxElementsInMemory="1000" eternal="false" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />

As per my understanding eternal =true attribute explains cache will never expire. But when i mention eternal =false, how long cache will remain active?

Also i have mentioned maxElementsInMemory=1000, what will happen when i have more than 1000 elements to put in cache. Will not they be stored in cache and discarded ?

Upvotes: 0

Views: 16456

Answers (3)

I'm very late to answer this question. But as i have also gone through this issue so want to share my experience.

Hi @emilly, I'm answering specifically to your questions and we will use config. that is shared by @RCB.

<cache name="customerList" maxElementsInMemory="1000" eternal="false" timeToLiveSeconds="60" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />

Question 1:

As timeToLiveSeconds="60" so it's the maximum time for which "customerList" data will be there in cache(L2). Cache Eviction is not directly related to the value of this attribute(timeToLiveSeconds).

e.g. If customerList is created on 12:00:00, it will be available till 12:00:59.

Question 2: say I want to add one more element after 1000. I cache will evict least recently used one and stored the latest element in list as I mentioned memoryStoreEvictionPolicy. Is that right ?

yes, this is the cache(L2) eviction and it will be according to the memoryStoreEvictionPolicy="LRU" and in this example it will a valid operation before TTL(60 seconds).

Upvotes: 0

Louis Jacomet
Louis Jacomet

Reputation: 14500

These two configuration options relate to two fundamental caching concepts:

  • Data freshness / Expiry: Data in cache can expire in order to guarantee that what the cache returns does not become stale. This is controlled in Ehcache 2.x by different attributes:
    • eternal attribute - if true indicates that mappings never expire,
    • time to live (TTL) - a duration after the cache mapping was created,
    • and/or time to idle (TTI) - a duration after the cache mapping was last accessed
  • Capacity control / Eviction: Limit the cache size so that you do not exhaust memory of your application by filling up a cache
    • When the maximum number of mappings is reached, the cache will start evicting entries - the exact entry evicted depends from the cache configuration / implementation
    • In Ehcache 2.x, maxElementsInMemory is a count based capacity control, there is also a memory size based option.

Upvotes: 2

RCB
RCB

Reputation: 845

When eternal is false, you can control how long items stay in cache by using the timeToLiveSeconds attribute. For example, for items to expire after 1 minute:

<cache name="customerList" maxElementsInMemory="1000" eternal="false" timeToLiveSeconds="60" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />

If you have more than 1000 items and have specified maxElementsInMemory=1000, then only 1000 of them will be stored in cache. maxElementsInMemory is an upper limit.

Upvotes: 0

Related Questions