GridGain Developers Hub
GitHub logo GridGain iso GridGain.com
GridGain Software Documentation

Expiry Policies

Overview

Expiry Policy specifies the amount of time that must pass before an entry is considered expired. Time can be counted from creation, last access, or modification time.

Depending on the memory configuration, the expiration policies will remove entries from either RAM or disk:

  • In-Memory Mode (data is stored solely in RAM): expired entries are purged from RAM.

  • In-Memory + Native persistence: expired entries are removed from both memory and disk. Note that expiry policies will remove entries from the partition files on disk without freeing up space. The space will be reused to write subsequent entries.

  • In-Memory + External Storage: expired entries are removed from memory only (in GridGain) and left untouched in the external storage (RDBMS, NoSQL, and other databases).

  • In-Memory + Swap: expired entries are removed from both RAM and swap files.

To set up an expiration policy, you can use any of the standard implementations of javax.cache.expiry.ExpiryPolicy or implement your own.

Configuration

Below is an example expiry policy configuration.

<bean class="org.apache.ignite.configuration.CacheConfiguration">

    <property name="expiryPolicyFactory">
        <bean class="javax.cache.expiry.CreatedExpiryPolicy" factory-method="factoryOf">
            <constructor-arg>
                <bean class="javax.cache.expiry.Duration">
                    <constructor-arg value="MINUTES"/>
                    <constructor-arg value="5"/>
                </bean>
            </constructor-arg>
        </bean>
    </property>

</bean>
CacheConfiguration cfg = new CacheConfiguration();

cfg.setName("myCache");
cfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES));
class ExpiryPolicyFactoryImpl: IFactory<IExpiryPolicy> {
    public IExpiryPolicy CreateInstance() {
        return new ExpiryPolicy(TimeSpan.FromMilliseconds(100),TimeSpan.FromMilliseconds(100),TimeSpan.FromMilliseconds(100));
    }
}

public static void Example()
{
    var cfg = new CacheConfiguration
    {
        Name = "cache_name",
        ExpiryPolicyFactory = new ExpiryPolicyFactoryImpl()
    };

You can also change or set Expiry Policy for individual cache operations. This policy will be used for each operation invoked on the returned cache instance.

IgniteCache<Object, Object> cache = ignite.getOrCreateCache("myCache").withExpiryPolicy(
    new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5))
);

Eager TTL

Entries that are expired can be removed from the cache either eagerly or when they are accessed by a cache operation. If there is at least one cache configured with eager TTL enabled, GridGain will create a single thread to clean up expired entries in the background.

If the property is set to false, expired entries will not be removed immediately. Instead, they will be removed when they are requested in a cache operation by the thread that executes the operation.

Eager TTL can be enabled or disabled via the CacheConfiguration.eagerTtl property (the default value is true):

<bean class="org.apache.ignite.configuration.CacheConfiguration">
    <property name="eagerTtl" value="true"/>
</bean>
CacheConfiguration cfg = new CacheConfiguration();

cfg.setName("myCache");

cfg.setEagerTtl(true);
var cfg = new CacheConfiguration {
    Name = "cache_name",
    EagerTtl = true
};