• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Expiration

NCache supports time based data invalidation where you can specify the time or interval to invalidate your cache data. This type of data invalidation is used if it changes to the data that occur in a deterministic time fashion or where you can determine the frequency of data change. For example, customer information or region information may not be frequently updated, so you can use a time based expiration to invalidate such data.

Expirations can either be absolute or sliding. Absolute expiration is added to an item by specifying the exact date and time at which item should be expired and removed from the cache. Sliding expiration is used for the cache to retain the data as long as it's being used by the application and throw away any data that has not been used for a specific period of time.

Prerequisites

  • Add the following Maven dependencies in your pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <!--for NCache Enterprise Edition--> 
    <artifactId>ncache-client</artifactId>
    <!--for NCache Professional Edition-->
    <artifactId>ncache-professional-client</artifactId>
    <version>x.x.x</version>
</dependency>
  • Import the following packages in your application:

    • import javax.cache.CacheManager;
    • import javax.cache.Caching;
    • import javax.cache.spi.CachingProvider;
  • Make sure that the data is serailized or registered with NCache Compact Serialization format.

  • Make sure that the cache is running.

Absolute Expiration

You can add/update items in the cache using absolute expiration.

Add Items with Absolute Expiration

In the following example, the add Operation is used to specify an absolute expiration time period for a cache item. The expiration time period is set by using the Calendar class. Also if Absolute Expiration is used then the NoSlidingExpiration parameter is explicitly declared.

try
{
    Product product = new Product();
    product.setProductID(1001);
    product.setProductName("Chai");
    String key = "Product:" + product.getProductID();

    //configure the expiration duration
    CompleteConfiguration<Object, Object> cacheConfig = new MutableConfiguration().setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES));

    //create cache with customized configuration settings
    javax.cache.Cache jCache = manager.createCache("mycache", cacheConfig);

    //add item to cache which will expire after 5 minutes
    jCache.put(key, product);
}
catch(Exception ex)
{
    // Handle Exception
}

Update Absolute Expiration in Cache

In the following example, the insert Operation is used to update the absolute expiration time period for an item added to the cache.

try
{
    Product product = new Product();
    product.setProductID(1001);
    product.setProductName("Chai");
    String key = "Product:" + product.getProductID();
    //configure the expiration duration
    CompleteConfiguration<Object, Object> cacheConfig = new MutableConfiguration().setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES));

    //create cache with customized configuration settings
    javax.cache.Cache jCache = manager.createCache("mycache", cacheConfig);

    //add item to cache which will expire after 5 minutes
    jCache.replace(key, product);
}
catch(Exception ex)
{
    // handle Exception
}

Sliding Expiration

Similar to absolute expiration, you can also add or update sliding expiration with an item.

Add Items with Sliding Expiration

In the following example, the add Operation is used to add a new item with the sliding expiration time period defined. The expiration time period is set by using the TimeSpan class. Also if the Sliding Expiration method is being used, NoAbsoluteExpiration needs to be explicitly declared in the parameters.

try
{
    Product product = new Product();
    product.setProductID(1001);
    product.setProductName("Chai");
    String key = "Product:" + product.getProductID();

    //configure the expiration duration
    CompleteConfiguration<Object, Object> cacheConfig = new MutableConfiguration().setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES));

    //create cache with customized configuration settings
    javax.cache.Cache jCache = manager.createCache("mycache", cacheConfig);

    //add item to cache which will expire after 5 minutes
    jCache.put(key, product);
}
catch(Exception ex)
{
    // Handle Exception
}

Update Item with Sliding Expiration

In this example, we will update a pre-existing item in the cache with sliding expiration.

try
{
    Product product = new Product();
    product.setProductID(1001);
    product.setProductName("Chai");
    String key = "Product:" + product.getProductID();

    //configure the expiration duration
    CompleteConfiguration<Object, Object> cacheConfig = new MutableConfiguration().setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES));

    //create cache with customized configuration settings
    javax.cache.Cache jCache = manager.createCache("mycache", cacheConfig);

    //add item to cache which will expire after 5 minutes
    jCache.replace(key, product);
}
catch(Exception ex)
{
    // Handle exception
}

See Also

Initialize Cache Add/Update in Cache
Clear Cache
Hibernate Caching
Event Notifications in Cache
NCache Java Session Module

Back to top Copyright © 2017 Alachisoft