Caching is a technique to store frequently accessed web pages(or data), temporarily in memory that are faster to access than accessing it from the original source. For example, when you access a web page for the first time, it takes a while to load. Subsequent visits to that website will be a lot faster because the web page has been cached on your computer.
You may cache a whole web page, parts of a web page, or application data according to your requirements. However you may not want certain items to be cached independently. For example, you are running a news website and you want the content on your website to be refreshed at regular intervals. You may want the content on your website to refresh every half an hour, ten minutes, or even every minute.
|Public Properties Of System.Web.Caching.Cache Class|
|Count||Obtains the number of items stored in the cache|
|EffectivePrivateBytesLimit||Obtains the number of kilobytes available for the cache|
|Item||Obtains the cache item at the specified key|
|Public Methods Of System.Web.Caching.Cache class|
|Add||Adds a specified item to the cache object|
|Get||Retrieves the specified item from the cache object|
|GetEnumerator||Retrieves a directory enumerator to iterate through the
key settings and their values contained in the cache
|Insert||Inserts an item into the cache object|
|Remove||Removes the specified item from the cache object|
Caching Techniques: Asp.Net 2.0 provides three types of caching techniques:
- Output Caching(also called as page level Caching)
- Partial-Page output Caching
- Data Caching
Output caching or page level caching is the simple technique of caching in ASP.NET. In output caching, if a web page is requested by the client machine for the first time, it is accessed from the web server and sent to the client machine, also a copy of the web page is stored in the cache. Now if the same web page is requested further, the web page is not accessed from the server instead the copy of the web page which is stored on the cache is sent to the client machine. If a copy of the web page is not found in the cache, the web page is accessed from the server and sent to the client machine, also a copy of the web page is stored in the cache for later use.
Output caching is also very useful when you have static pages. For example, in cases where the content of the published article on your website does not change, using output cache will be very effective. This is because the engine will cache everything that is rendered(or displayed) on the web page. For subsequent requests, it will send a copy of the web page which is stored in the cache to the client machine and thus it reduces the load on the web server because the process doesn't get busy in rendering the page every time a request comes in.
You can easily implement output caching by just adding the @ OutputCache directive in the web pages as follows:
< %@ outputCache duration="10" VaryByParam="none" % >You can also define as to where you want to cache the data either on the client-side or the server-side, or on the proxy server by including a location attribute:
< %@ outputcache duration="90" location="Any | client |" + "downstream | server None" VaryByParam="none" %>Partial-Page Output Caching: It is not always that you require caching the complete page. Sometimes you need to implement caching on only some of the contents of a web page. Other contents of the web page will be refreshed each time the page is requested. This is called partial caching or fragment caching.
Remaining topics with examples are explained in other article.
Comments/Suggestions are invited. Happy coding......!