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 discuss about
1. When and how to use "CacheItemRemovedCallback" delegate
2. Reloading or refreshing cache automatically, when cached data has expired
Code samples used in the demo, is available on my blog at the following link
Explanantion of code in btnLoadCountriesAndCache_Click() event handler:
When you click "Load Countries & Cache" button, the xml data is read from "Countries.xml" into a dataset, which is then cached. Notice that, before we cache data using the "Cache" objects "Insert()" method, we are creating an instance of CacheItemRemovedCallback delegate. To the constructor of this delegate, we are passing, the name of the function that we want to have executed automatically, when the cache item is removed from cache. An item may be removed automatically, from cache, when any of the following conditions are true
The cached item has expired.
The cache is full.
There is a cache dependency, and the item, that the cache object is dependent on has changed.
The delegate object is then passed as an argument for CacheItemRemovedCallback delegate parameter of the insert() method. So, when the item, with cache key"CountriesData" is removed the delgate gets invoked, which inturn will automatically invoke, the function it is pointing to, in our case "CacheItemRemovedCallbackMethod()".
Explanantion of code in CacheItemRemovedCallbackMethod() function:
This method gets invoked automatically, whenever the cached item is removed from cache. So, this is the opportunity for us to decide what we want to do when the cached item is removed from cache. For example, in this method the following 2 lines get the key of the item that is removed from the cache and the removed reason, and stores the message in another cache object, with key"CacheStatus".
string dataToStore"Cache item with key\"" + key + "\" is no longer present. Reason" + reason.ToString();
Alternatively we can also store information about the removed cach object, in a database table. The ado.net code that does this is commented.
Finally we can also reload data ino cache, from the XML file. The code to reload data into cache, is also commented.
Explanantion of code in btnGetCountriesFromCache_Click() event handler:
The code in "Get Countries from Cache" button click event handler is staright forward. We check if countries data is present in cache, and if it is, we retrieve the data from cache and display in gridview control.
Code in btnRemoveCachedItem_Click() and btnGetCacheStatus_Click() is self explanatory.