Using Absolute Expiration
To utilize the APIs, include the following namespace in your application:
Alachisoft.NCache.Web.Caching.
Adding New Item with Absolute Expiration
In the following example, the Add Operation is used to specify an absolute expiration time period for a cache item.
try
{
//adding absolute expiration of 5 min through Add API.
cache.Add(key, product, null, System.DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Default);
// adding Default Absolute expiration with configured value
cache.Add(key, product, null, Cache.DefaultAbsolute, Cache.NoSlidingExpiration, CacheItemPriority.Default);
// adding AbsoluteLonger expiration with configured value
cache.Add(key, product, null, Cache.DefaultAbsoluteLonger, Cache.NoSlidingExpiration, CacheItemPriority.Default);
}
catch (Exception ex){
// handle exception
}
Updating Data with Absolute Expiration
In the following example, the Insert Operation is used to add/update the absolute expiration time period for an item added to the cache.
try
{
// updating absolute expiration of 5 min through Add API.
cache.Insert(key, product, null, System.DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Default);
//updating Default Absolute expiration with configured value
cache.Insert(key, product, null, Cache.DefaultAbsolute, Cache.NoSlidingExpiration, CacheItemPriority.Default);
//updating AbsoluteLonger expiration with configured value
cache.Insert(key, product, null, Cache.DefaultAbsoluteLonger, Cache.NoSlidingExpiration, CacheItemPriority.Default);
}
catch (Exception ex){
// handle exception
}
Setting Absolute Expiration with CacheItem
In the following example Absolute Expiration time period is set by assigning it
to a property of a CacheItem
object.
// Setting Absolute expiration of 5 minutes.
cacheItem.AbsoluteExpiration = System.DateTime.Now.AddMinutes(5);
try{
cache.Add(key, cacheItem, DSWriteOption.None, null);
}
catch (Exception ex){
// handle exception
}