Configure Data Expiration
Configure the type of expiration and the expiration action to use.
Set the region's
statistics-enabledattribute to true.The statistics used for expiration are available directly to the application through the
CacheStatisticsobject returned by theRegionandRegion.EntrygetStatisticsmethods. TheCacheStatisticsobject also provides a method for resetting the statistics counters.Set the expiration attributes by expiration type, with the max times and expiration actions. See the region attributes listings for
entry-time-to-live,entry-idle-time,region-time-to-live, andregion-idle-timein <region-attributes>.For partitioned regions, to ensure reliable read behavior, use the
time-to-liveattributes, not theidle-timeattributes. In addition, you cannot uselocal-destroyorlocal-invalidateexpiration actions in partitioned regions.Replicated regions example:
// Setting standard expiration on an entry <region-attributes statistics-enabled="true"> <entry-idle-time> <expiration-attributes timeout="60" action="local-invalidate"/> </entry-idle-time> </region-attributes>Override the region-wide settings for specific entries, if required by your application. To do this:
Program a custom expiration class that implements
org.apache.geode.cache.CustomExpiry. Example:// Custom expiration class // Use the key for a region entry to set entry-specific expiration timeouts of // 10 seconds for even-numbered keys with a DESTROY action on the expired entries // Leave the default region setting for all odd-numbered keys. public class MyClass implements CustomExpiry, Declarable { private static final ExpirationAttributes CUSTOM_EXPIRY = new ExpirationAttributes(10, ExpirationAction.DESTROY); public ExpirationAttributes getExpiry(Entry entry) { int key = (Integer)entry.getKey(); return key % 2 == 0 ? CUSTOM_EXPIRY : null; } }- Define the class inside the expiration attributes settings for the region. Example:
``` pre
<!-- Set default entry idle timeout expiration for the region -->
<!-- Pass entries to custom expiry class for expiration overrides -->
<region-attributes statistics-enabled="true">
<entry-idle-time>
<expiration-attributes timeout="60" action="local-invalidate">
<custom-expiry>
<class-name>com.company.mypackage.MyClass</class-name>
</custom-expiry>
</expiration-attributes>
</entry-idle-time>
</region-attributes>
```
You can also configure Regions using the gfsh command-line interface, however, you cannot configure custom-expiry using gfsh. See Region Commands.