Cache Expiration Example

Error formatting macro: composition-setup: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil

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. Read more about expiration. See also Adding an Entry, Updating an Entry, Accessing an Entry.

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.

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

In a single session, run the example:

$ java quickstart.DataExpiration

Example Listings

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

Error formatting macro: deck: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">
    
<!--
  | DataExpiration.xml
  |
  | Configures a region with entry idle time expiration.
 -->
<cache>
	<region name="exampleRegion">
		<region-attributes>
			<entry-idle-time>
				<expiration-attributes timeout="10" action="destroy" />
			</entry-idle-time>
			<cache-listener>
				<class-name>quickstart.SimpleCacheListener</class-name>
			</cache-listener>
		</region-attributes>
	</region>
</cache>
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import java.util.Arrays;

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.ExpirationAttributes;
import com.gemstone.gemfire.cache.Region;

/**
 * Each data region can be configured to expire entries that have not been accessed recently or that have not been
 * updated recently. You can configure eviction to remove the data and its key (destruction) or just the data
 * (invalidation). Please refer to the quickstart guide for instructions on how to run this example.
 *
 * @author GemStone Systems, Inc.
 * @since 4.1.1
 */
public class DataExpiration {

	public static void main(String[] args) throws Exception {

		System.out.println("This example shows entry expiration.");
		System.out.println();
		System.out.println("Connecting to the distributed system and creating the cache.");
		// Create the cache which causes the cache-xml-file to be parsed
		Cache cache = new CacheFactory()
                  .set("name", "DataExpiration")
                  .set("cache-xml-file", "xml/DataExpiration.xml")
                  .create();

		// Get the exampleRegion
		Region<String, String> exampleRegion = cache.getRegion("exampleRegion");
		System.out.println();
		System.out.println("Example region \"" + exampleRegion.getFullPath() + "\" created in cache.");

		// Get the EntryIdleTimeout setting from the region attributes
		ExpirationAttributes expirationAttr = exampleRegion.getAttributes().getEntryIdleTimeout();

		System.out.println();
		System.out.println("The region \"" + exampleRegion.getFullPath() + "\" is configured to");
		System.out.println(expirationAttr.getAction() + " any cache entry that is idle for ");
		System.out.println(expirationAttr.getTimeout() + " seconds.");

		int idleTime = expirationAttr.getTimeout();

		System.out.println();
		System.out.println("Putting entry: key1 => value1");
		exampleRegion.put("key1", "value1");
		System.out.println("Putting entry: key2 => value2");
		exampleRegion.put("key2", "value2");
		System.out.println("Putting entry: key3 => value3");
		exampleRegion.put("key3", "value3");

		System.out.println();
		System.out.println("The cache now contains:");
		printRegionContents(exampleRegion);

		System.out.println();
		System.out.println("Before the idle time expiration, access two of the entries...");
		Thread.sleep((idleTime - 1) * 1000);

		// Get key1 to prevent it from expiring
		System.out.println("Getting entry: key1 => " + exampleRegion.get("key1"));

		// Update key2 to prevent it from expiring
		System.out.println("Putting entry: key2 => value2000");
		System.out.println();
		exampleRegion.put("key2", "value2000");

		System.out.println("Next, the listener should report on an expiration action... ");
		System.out.println();

		// Sleep past expiration
		Thread.sleep((idleTime / 2) * 1000);

		System.out.println("After the expiration timeout, the cache contains:");
		printRegionContents(exampleRegion);

		System.out.println();
		System.out.println("Closing the cache and disconnecting.");
		cache.close();
	}

	private static void printRegionContents(Region<?,?> region) {
		Object[] keys = region.keySet().toArray();
		Arrays.sort(keys);

		for (Object key : keys) {
			System.out.println("    " + key + " => " + region.get(key));
		}
	}
}
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import com.gemstone.gemfire.cache.Declarable;
import com.gemstone.gemfire.cache.EntryEvent;
import com.gemstone.gemfire.cache.RegionEvent;
import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
import java.util.Properties;

public class SimpleCacheListener<K,V> extends CacheListenerAdapter<K,V> implements Declarable {

  public void afterCreate(EntryEvent<K,V> e) {
    System.out.println("    Received afterCreate event for entry: " +
      e.getKey() + ", " + e.getNewValue());
  }
  
  public void afterUpdate(EntryEvent<K,V> e) {
    System.out.println("    Received afterUpdate event for entry: " +
      e.getKey() + ", " + e.getNewValue());
  }
  
  public void afterDestroy(EntryEvent<K,V> e) {
    System.out.println("    Received afterDestroy event for entry: " +
      e.getKey());
  }

  public void afterInvalidate(EntryEvent<K,V> e) {
    System.out.println("    Received afterInvalidate event for entry: " +
      e.getKey());
  }

  public void afterRegionLive(RegionEvent e) {
    System.out.println("    Received afterRegionLive event, sent to durable clients after \nthe server has finished replaying stored events.  ");
  }

  public void init(Properties props) {
    // do nothing
  }
}

Related Javadocs

Expiration methods on RegionFactory

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.