Wednesday 5 November 2014

What is Caching



It’s nothing but a thought kind of memory. In respect to asp.net it's the memory of the machine/server from where the source-code is running. It is the one way which allows storing complex data for reusability.

Now think criteria where clients access an ASP.NET page, there are basically two ways to provide them with the information they need:

· The ASP.NET page can either obtain information from server resources, such as from data that has been persisted to a database, or
· the ASP.NET page can obtain information from within the application.

Retrieving information from a resource outside the application will require more processing steps, and will therefore require more time and resources on the server than if the information can be obtained from within the application space.

Now, suppose the information's which sent to browsers have already been prepared then how faster the process of web-page.

The ASP.NET 3.5 Framework supports the following types of caching:

Page Output Caching:

Page Output Caching caches an entire page. 

Partial Page Caching:

Partial Page Caching enables you to get around this problem by enabling you to cache only particular regions of a page.

DataSource Caching:

You use DataSource Caching with the different ASP.NET DataSource controls such as the SqlDataSource and ObjectDataSource controls. When you enable caching with a DataSource control, the DataSource control caches the data that it represents.

Data Caching:

Finally, Data Caching is the fundamental caching mechanism. Behind the scenes, all the other types of caching use Data Caching. You can use Data Caching to cache arbitrary objects in memory. For example, you can use Data Caching to cache a Dataset across multiple pages in a web application.

Note:
The Cache object can also have an expiration which would allow us to reinstitute data into the memory in intervals. Using the same example as above, we can make the cache expire every two hours, and repopulate the data. It would do this every 2 hours throughout the day, allowing the most up to date data to be fetched. Below is an example of how something can be put into the cache:

Referenced from msdn 

http://msdn.microsoft.com/en-us/library/system.web.caching.cache(VS.80).aspx

public void AddItemToCache(Object sender, EventArgs e)
{
itemRemoved = false;
onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
if (Cache["Key1"] == null)
Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60),TimeSpan.Zero, CacheItemPriority.High, onRemove);
}


public void RemoveItemFromCache(Object sender, EventArgs e)
{
if(Cache["Key1"] != null)
Cache.Remove("Key1");
}

0 comments:

Post a Comment