- Expiration is a top-level construct, represented in the configuration as well as in the cache API. More on this later.
- While eviction is local to each cache instance , expiration is cluster-wide . Expiration lifespans and maxIdle values are replicated along with the cache entry.
- Expiration lifespan and maxIdle are also persisted in CacheStores, so this information survives eviction/passivation.
- Three eviction strategies are shipped, EvictionStrategy.NONE, EvictionStrategy.FIFO and EvictionStrategy.LRU, as these are deemed the most useful. If there are sufficient requests for more strategies these may be added later.
- Architecturally, this is all-new from ground-up.
Eviction may be configured using the Configuration bean or the XML file. Eviction configuration is on a per-cache basis. Valid eviction-related configuration elements are:
|XML changes in Infinispan 5.0|
From Infinispan 5.0 onwards, wakeupInterval attribute has been moved to expiration XML element. This is because since 4.1, eviction happens in the user thread, and so the old eviction thread now simply purges expired entries from memory and any attached cache store. So, effectively, wakeUpInterval controls how often this purging occurs:
Programmatically, the same would be defined using:
|New fluent configuration in Infinispan 5.0|
Since 5.0 onwards a brand new, more fluent, programmatic configuration is available which allows the above configuration to be expressed in the following way:
Eviction is disabled by default. If enabled (using an empty <eviction /> element), certain default values are used:
- strategy: EvictionStrategy.NONEis assumed, if a strategy is not specified..
- wakeupInterval: 5000 is used if not specified.
- If you wish to disable the eviction thread, set wakeupInterval to -1.
- maxEntries: -1 is used if not specified, which means unlimited entries.
- 0 means no entries, and the eviction thread will strive to keep the cache empty.
Expiration lifespan and maxIdle both default to -1.
Expiration allows you to set either a lifespan or a maximum idle time on each key/value pair stored in the cache. This can either be set cache-wide using the configuration, as described above, or it can be defined per-key/value pair using the Cache interface. Any values defined per key/value pair overrides the cache-wide default for the specific entry in question.
For example, assume the following configuration:
Central to eviction is an EvictionManager - which is only available if eviction or expiration is configured.
The purpose of the EvictionManager is to drive the eviction/expiration thread which periodically purges items from the DataContainer. If the eviction thread is disabled (wakeupInterval set to -1) eviction can be kicked off manually using EvictionManager.processEviction(), for example from another maintenance thread that may run periodically in your application.
The eviction manager processes evictions in the following manner:
- Causes the data container to purge expired entries
- Causes cache stores (if any) to purge expired entries
- Prunes the data container to a specific size, determined by maxElements