Cache Eviction Example

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

You can use eviction to control how much heap your data regions use. Eviction controls your data region size by removing least recently used (LRU) entries to make way for new data. It kicks in when your data region reaches a specified size or entry count. Size can be absolute or a percentage of your VM's current heap. Read more about eviction.

Running the Example

In this example, the data region is configured to keep the entry count at 10 or below by destroying LRU entries. A cache listener installed on the region reports the changes to the region entries.

This example requires one terminal session configured according to the instructions in Setting Up GemFire.

In a single session, run the example:

$ java quickstart.DataEviction

Example Listings

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

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"?>
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"

  | DataEviction.xml
  | Configures a region to destroy entries when the region reaches 
  | a certain capacity. Includes a listener to report on the activity. 
	<region name="exampleRegion">
				<lru-entry-count maximum="10" />
				<!-- entry destruction is the default eviction action -->
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

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

 * This example shows cached data eviction. Use eviction to keep a region size in check when you can easily get the data
 * again from an outside source. If you have data that is hard to retrieve again, you might want to use data overflow.
 * (See the DataOverflow example.) The mechanism for deciding when and what to remove from memory is the same for
 * overflow and standard eviction. Standard eviction just destroys the entry instead of copying it out to disk. Both use
 * a Least Recently Used (LRU) eviction controller to decide what to remove from memory. Please refer to the quickstart
 * guide for instructions on how to run this example.
 * @author GemStone Systems, Inc.
 * @since 5.8
public class DataEviction {

	public static void main(String[] args) throws Exception {
		System.out.println("This example keeps the region size below 10 entries by destroying the ");
		System.out.println("least recently used entry when an entry addition would take the count");
		System.out.println("over 10.");
		System.out.println("You can set capacity limits based on entry count, absolute region size,");
		System.out.println("or region size as a percentage of available heap.");

		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", "DataEviction")
                  .set("cache-xml-file", "xml/DataEviction.xml")

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

		System.out.println("Putting 12 cache entries into the cache. The listener will report on");
		System.out.println("the puts and on any destroys done by the eviction controller.");
		for (long i = 1; i < 13; i++) {
			exampleRegion.put("key" + i, "value" + i);
		// Close the cache and disconnect from GemFire distributed system
		System.out.println("Closing the cache and disconnecting.");
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: " +

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

  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


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