Cache Expiration Example

Expiration helps you ensure that your data is current and pertinent by removing old and unused entries from the cache. It can also give you some control over cache size. When you try to access an expired entry, the value is not there and your cache looks to another source for a fresh update. Usually it calls the loader you have installed on the data region.

You can expire entries based on how recently they were added or updated, or based on how recently they were accessed by your application, added, or updated. Add/update-based expiration, called time-to-live or TTL, keeps your application from accessing stale data. Add/update/access-based, or idle-time, expiration keeps your application from accessing stale data and from using memory for data it isn't interested in accessing.

Running the Example

This example uses idle time expiration, configured through the XML file to destroy expired entries. The program creates three entries and then waits beyond the expiration time. During the wait time, the program updates one entry and accesses another. When the expiration time elapses, only the third, unaccessed and unmodified entry is expired. The cache listener, installed on the region, reports on all changes to the entries.

Note: To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up Your Environment.

In a single session, run the example:

$ java quickstart.DataExpiration

Example Source Files

Program and cache configuration source files for the example, including the listener declared in DataExpiration.xml:

Table 1. Cache configuration files, located in $SamplesDirectory/quickstart/xml
DataExpiration.xml Configures a region with entry idle time expiration.
Table 2. Java program files, located in $SamplesDirectory/quickstart/quickstart Adds data to the cache and lists entries before and after expiration. A CacheListener that reports cache events.
Related Topics

Related Javadocs