Text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
All ASP .NET Text Articles
All ASP .NET Slides
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
In this video we will dicsuss about AbsoluteExpiration, SlidingExpiration, and CacheItemPriority parameters of the Insert() and Add() methods of cache object.
When you cache an item using the Insert() or Add() method, you can also speicfy, how long you want the item to be cached. There are 2 ways to do this.
AbsoluteExpiration: A DateTime object that specifies when the data should be removed from the cache. When you specify "AbsoluteExpiration", the cache item will expire at that time, irrespective of whether the cached item is accessed or not. For example, the following line will cache dataset, ds, for 10 seconds, irrespective of whether the dataset, is accessed or not within those 10 seconds, after it is cached. Since we are using absolute expiration, we specified Cache.NoSlidingExpiration for "SlidingExpiration" parameter of the Insert() method.
Cache.Insert("ProductsData", ds, null, DateTime.Now.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration);
SlidingExpiration: A TimeSpan object that identifies how long the data should remain in the cache after the data was last accessed. For example, the following line will cache dataset, ds, for 10 seconds. If the dataset is accessed from the cache with in the specified 10 seconds, then, from that point, the dataset will remain in cach for the next 10 seconds. Since we are using sliding expiration, we specified Cache.NoAbsoluteExpiration for "AbsoluteExpiration" parameter of the Insert() method.
Cache.Insert("ProductsData", ds, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));
What happens if you specify both, AbsoluteExpiration and SlidingExpiration when caching application data?
You get a run time exception stating 'absoluteExpiration must be DateTime.MaxValue or slidingExpiration must be timeSpan.Zero'
CacheItemPriority: Sliding expiration and absolute expiration can be used to control, how long the item is cached, but please note, if the web server is running low on memory, and if it requires memory, it may remove cached items that may not have expired. However, the order in which the items are removed is determined by the cached item's priority. Cache item's priority can be specified using CacheItemPriority enum.
CacheItemPriority enum values:
For example, let us say we have 3 items with the following priorities and they are cached.
Item 1 with CacheItemPriority.NotRemovable
Item 2 with CacheItemPriority.High
Item 3 with CacheItemPriority.AboveNormal
Now, if the server is running low on memory, it may remove the items from cache, even if they have not expired. Since, Item 3 has the least priority, it will be removed first, followed by Item 2 and then finally Item 1.