Different ways to cache application data in asp net Part 127

  • دعنا نشارك هذا :
ترتيب الدرس : 127
المدة : 00:13:19
التحكم :
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 Part 126, of the asp.net video tutorial we discussed about, "Caching application data" using direct assignment as shown below. Please watch Part 126, from the asp.net video tutorial before proceeding.

Link for asp.net tutorial

In this video we will discuss about
1. Different ways to cache application data
2. Removing an item from cache
3. Similarities and difference between cache and application state

Apart from caching data, using assignment, there are 2 other ways to cache application data
1. Using "Cache" object's Insert() method
2. Using "Cache" object's Add() method

Caching application data, using "Cache" object's Insert() method:
Cache object's, Insert() method has got 5 overloaded versions. Depending on application requirement, we can choose the overloaded version that best suits our needs. The simplest overloaded version, takes 2 parameters.
1. The Cache Key and
2. The data that we want to cache

In Part 126, we cached products dataset, using assignment as shown below

The same thing can be achieved, using cache object's "Insert()" method, as shown below.
Cache.Insert("ProductsData", ds);

We will discuss about other overloaded versions of "Insert()" method in a later video session.

Caching application data, using "Cache" object's Add() method:
Adding application data, to the cache object, using "Add()" method is similar to "Insert()" method. As "Insert()" method has got several overloaded versions, some of the parameters are optional. Where as, "Add()" method does not have any overloads, and hence, all parameters must be specified.
Cache.Add("ProductsData", ds, null, System.Web.Caching.Cache.NoAbsoluteExpiration,System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);

We will discuss about "absolute expiration", "sliding expiration", and CacheItemPriority in a later video session.

Be cautious when dealing with cache keys: Assigning a value to a cache key, that is already being used will silently overwrite the existing value, without any warnings. For example
The value stored in "MyKey" is "Value1"
Cache["MyKey"]"Value 1";

The following line will silently overwrite "MyKey" value to "Value 2"
Cache["MyKey"]"Value 2";

To Remove an item from cache explicitly, use Remove() method. The following line would remove the cache item with key "MyKey"

Please Note: 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.

In a way, Cache object is similar to Application state. The objects that we store in application state variables are available anywhere within a Web application. Objects stored in cache are also available anywhere within a Web application. But the difference is that, items stored in cache can expire, where as items in application state will never expire.

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

Sep 23, 2020 07:32:26