Reload data into cache automatically when data in the table changes Part 132

  • دعنا نشارك هذا :
ترتيب الدرس : 132
المدة : 00:12:11
التحكم :
Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/02/reload-data-into-cache-automatically.html

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.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1

Slides
http://csharp-video-tutorials.blogspot.com/2013/08/part-132-reload-data-into-cache.html

All ASP .NET Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-aspnet-video-tutorial.html

All ASP .NET Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-slides.html

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view1&sortdd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

In Part 130 of asp.net video tutorial, we discussed about reloading data into cache automatically, when the xml file from which the data was initially retrieved, has changed.

In this video, we will discuss about reloading data into cache automatically when data in the underlying database table has changed. Please watch Part 131, before proceeding with this video.

To load data automatically into cache, when cached data is removed, we need to define a callback method. This callback method will be invoked when the respective item is removed from cache. So, the code to reload and re-cache data can be written in this method. The callback method signature should match, with the signature of "CacheItemRemovedCallback" delegate. The call back method is defined below.
public void CacheItemRemovedCallbackMethod(string key, object value, CacheItemRemovedReason reason)
{
string CSConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;

SqlConnection connew SqlConnection(CS);
SqlDataAdapter danew SqlDataAdapter("spGetProducts", con);
da.SelectCommand.CommandTypeCommandType.StoredProcedure;
DataSet dsnew DataSet();
da.Fill(ds);

CacheItemRemovedCallback onCacheItemRemovednew CacheItemRemovedCallback(CacheItemRemovedCallbackMethod);

SqlCacheDependency sqlDependencynew SqlCacheDependency("Sample", "tblProducts");
Cache.Insert("ProductsData", ds, sqlDependency, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration,
CacheItemPriority.Default, onCacheItemRemoved);
}

Now, create an instance of "CacheItemRemovedCallback" delegate, and to the contructor pass the name of the callback method, that should be executed automatically when, the cached item is removed.
CacheItemRemovedCallback onCacheItemRemovednew CacheItemRemovedCallback(CacheItemRemovedCallbackMethod);

In Part 131, to cache data, we used cache object's, Insert() method that takes 3 parameters, as shown below.
Cache.Insert("ProductsData", ds, sqlDependency);

Instead, let's use the overloaded version that takes 7 parameters and pass "onCacheItemRemoved" as an argument for "CacheItemRemovedCallback" parameter.
Cache.Insert("ProductsData", ds, sqlDependency, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration,
CacheItemPriority.Default, onCacheItemRemoved);

That's it. We are done. Now, please run the application. When "Get Data" button is clicked for the first time, the data is loaded from database. Once the data is loaded into cache, we always get it from cache when we click "Get Data". Now, execute an update statement on the database table. Notice that, when we click "Get Data" button now, we still get data from cache, but notice that, the updated data is loaded.

سيتم تفعيل التعليقات قريبا

y
Sep 23, 2020 07:32:26
youtube