Alachisoft NCache 4.1 - Online Documentation

Expiring Cached Items

 
An expiration object can be associated with each item being added to the cache. When that expiration object invalidates (invalidation of expiration object is dependent on the provided parameters), the associated item is removed from the cache. NCache provides two types of expirations i.e. Absolute expiration and Sliding expiration.
 
  1. Include the following import statements in your project:
     
    import com.alachisoft.ncache.web.caching.*;
     
  2. Create a new Class named 'Customer' in your already created Test Application.
  3. Insert the following code in the 'Customer' class.
     
    import java.io.Serializable;
    public class Customer implements Serializable
    {
    }
     
  4. Now insert the following code in the 'Main' of the project.
     
    Absolute Expiration:
     
    The time period at which the added object expires and is removed from the cache. If you are using sliding expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.
     
    Cache _cache = NCache.initializeCache("myCache");
    _cache.clear();
     
    Customer customer = new Customer();
    customer.name = "David";
    customer.customerID = 1001;
    customer.setCity("London");
    customer.setContactNumber("+44-xxx-xxxx-xxxx");
    customer.setOrderInProcess(true);
    Calendar calendar = Calendar.getInstance();
    calendar.add(calendar.MINUTE, 10);
     
    _cache.add("Customer:David:1001", customer, null, calendar.getTime(), Cache.NoSlidingExpiration, CacheItemPriority.Normal);
     
    Thread.sleep(1000*60*11);
     
    Object object_Customer = _cache.get("Customer:David:1001");
    Customer customerInformation = (Customer) object_Customer;
    System.out.println("Customer ID: " + customerInformation.customerID);
    System.out.println("Name: " + customerInformation.name);
    System.out.println("City: " + customerInformation.getCity());
    System.out.println("Contact Number: " + customerInformation.getContactNumber());
    System.out.println("Order placed: " + customerInformation.isOrderInProcess());
     
    Sliding Expiration:
     
    Time interval between the last accessed time of the added object and object expiration time. If this value is the equivalent of 10 minutes, the object expires and is removed from the cache 10 minutes after it is last accessed. If you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.
     
    Cache _cache = NCache.initializeCache("myCache");
    _cache.clear();
     
    Customer customer = new Customer();
    customer.name = "David";
    customer.customerID = 1001;
    customer.setCity("London");
    customer.setContactNumber("+44-xxx-xxxx-xxxx");
    customer.setOrderInProcess(true);
     
    _cache.add("Customer:David:1001", customer, null, Cache.NoAbsoluteExpiration, new TimeSpan(10,0), CacheItemPriority.Normal);
     
    Thread.sleep(1000*60*10);
     
    Object object_Customer = _cache.get("Customer:David:1001");
    Customer customerInformation = (Customer) object_Customer;
    System.out.println("Customer ID: " + customerInformation.customerID);
    System.out.println("Name: " + customerInformation.name);
    System.out.println("City: " + customerInformation.getCity());
    System.out.println("Contact Number: " + customerInformation.getContactNumber());
    System.out.println("Order placed: " + customerInformation.isOrderInProcess());
     
  5. Start the Local Cache 'myCache'.
  6. Run the project.
 
 
Using Absolute and Sliding Expiration
 
While adding some item to the cache, you can provide a DateTime value which is translated into Absolute Expiration and the item is removed from the cache at the specified DateTime value. If you don't want to provide any Absolute Expiration, you can use an enumeration (NoAbsoluteExpiration) provided by NCache. Similarly, you can provide a TimeSpan value which is translated into Sliding Expiration and item is removed from the cache if the specified period of time has elapsed since it was last accessed. If you don't want to provide a Sliding Expiration, you can use an enumeration (NoSlidingExpiration) provided by NCache.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.