1. Absolute Expiration
In absolute expiration cached item is removed after absolute interval. This interval starts when an item is added into the cache, and its duration is specified in parameters provided on cache insertion.
|
If Absolute Expiration is being used, then sliding expiration should be turned off by specifying NoSlidingExpiration parameter.
|
In this example
Add Operation provided by NCache is used to add absolute expiration to item. The expiration is set by using
DateTime class. Also if Absolute expiration is used,
NoSlidingExpiration is to be specified.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
string key = "Product:" +product.ProductID ;
try
{
// adding absolute expiration of 5 min through Add API.
cache.Add(key, product, null, System.DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal);
}
catch(OperationFailedException ex)
{
// handle exception
}
In this example,
Insert Operation provided by NCache is used to add/update absolute expiration to item added to the key. The expiration is set by using
DateTime class. Also as absolute expiration is being used,
NoSlidingExpiration is used for sliding expiration.
try
{
// adding absolute expiration of 5 min through Add API.
cache.Insert(key, product, null, System.DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal);
}
catch(OperationFailedException ex)
{
// handle exception
}
In this example expiration is set by assigning it as a property of CacheItem.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
CacheItem cacheItem = new CacheItem(product);
string key = "Product:" +product.ProductID ;
// Setting Absolute expiration of 10 minutes.
cacheItem.AbsoluteExpiration = System.DateTime.Now.AddMinutes(10);
try
{
cache.Add(key, cacheItem, DSWriteOption.None, null);
}
catch(OperationFailedException ex)
{
// handle exception
}
2. Sliding Expiration
In Sliding expiration, time for item expiration is not fixed. It is an interval between last access time of item and current
time. If this elapsed time interval is equal to defined sliding expiration settings then this item is removed from cache.
As sliding expiration means expire after a period of inactivity and is good for session and other temporary data that
must be removed if not used.
|
If sliding expiration is being used, the absolute Expiration should be turned off by specifying
NoAbsoluteExpiration.
|
In this example,
Add operation provided by NCache is used to add absolute expiration to item. The expiration is set by using
TimeSpan class. Also while using sliding expiration,
NoAbsoluteExpiration is used for absolute expiration.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
string key = "Product:" +product.ProductID ;
try
{
// adding sliding expiration of 5 sec through Add API.
cache.Add(key, product, null, Cache.NoAbsoluteExpiration,new TimeSpan(0, 0, 5), CacheItemPriority.Normal);
}
catch(OperationFailedException ex)
{
// handle exception
}
In this example,
Insert operation provided by NCache is used to add/update sliding expiration to item added to the cache. The
expiration is set by using
TimeSpan class. Also sliding expiration is the selected mode of expiration,
NoAbsoluteExpiration is used for absolute expiration.
try
{
// adding sliding expiration of 5 sec through Insert API.
cache.Insert(key, product, null, Cache.NoAbsoluteExpiration,new TimeSpan(0, 0, 5), CacheItemPriority.Normal);
}
catch(OperationFailedException ex)
{
// handle exception
}
In this example expiration is set by assigning it as a property of CacheItem.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
CacheItem cacheItem = new CacheItem(product);
// Setting sliding expiration of 10 seconds.
cacheItem.SlidingExpiration = new TimeSpan(0, 0, 10);
string key = "Product:" +product.ProductID ;
try
{
cache.Add(key, cacheItem, DSWriteOption.None, null);
}
catch(OperationFailedException ex)
{
// handle exception
}
See Also