i

ASP.Net A Complete Guide

Different Types Of Caching

ASP.NET provides the different types of caching like listed below:

  • Page-Level Caching/Page Output Caching

  • Page Fragment Caching/ fragment caching

  • Application Caching/data caching

Page-Level Caching/Page Output Caching:

In this caching technique, we can store the complete page in the memory. If we want to cache the page's output, we need to specify an @OutputCache directive at the top of the page.

Example: <%@ OutputCache Duration=8 VaryByParam="None" %>

Duration Attribute:

The value of these attributes is in second. It specifies how to log the output can be cached in the memory. After the specified duration elapsed, then the output from the cached gets cleared and for the immediate next requests the generated result. Output is cached in the memory.

VaryByParam Attribute:

This is the mandatory attribute. It specifies that the queryStringtring parameters to vary the cache.

VaryByParam will have below values:

VaryByParam="None" : Here, "None" specifies that page content can be cached regardless of what value passed to the attribute "varyByParam".

 VaryByParam="*": As we know, we can pass multiple parameters to the queryString, So this "*" value specifies that output of the can be cached for all the parameters.

 VaryByParam="browser": In some cases, generation output for the pages may vary in such case .net has provided the provision to cache the output and that output we can call as the browser-specific, and we can cache them.

 VaryByParam="{querystring parameter like id}":  Here, the value "Id" specifies that the page cache depends on the "Id" I mean for every different id will cache the page.

Page Fragment Caching/ fragment caching:

Fragment means a small piece or part. Sometimes we need to complete to cached the page, but we need to cache the certain part of a page, and that is common across all the sites, or it may be the same for a certain time period than in that case fragment will be helpful.

The syntax will be the same as the page caching.

<%@ OutputCache Duration=8 VaryByParam="None" %>

<%@ OutputCache Duration = 6 VaryByParam = "None" VaryByCustom = "Browser" %>

Application Caching/Data caching:

Asp.net supports caching the data as objects in memory. This cache is available as a page property. We can implement this feature by using the "cache" class. Once we stored the Data/object in memory, then we can use that in across the application. The lifespan of these techniques is the same as the lifespan of the form.