asp.net the majority [if not all] of the "pages in a" dynamic website are dynamic. This is, pages this are created on user request. As we know, Dynamic Web pages help-provide dynamic content, customized for the user requesting the page [e.g.: the User ' s home page. Dynamic pages also help provide dynamic content fetched to changing data store without the need for the administrator To change the page content every time something changes in the data store [e.g.: Listing, books in a Publisher ' s website ]. The disadvantage is the "overhead in creating" pages for every user request.
To overcome this, some websites have page creation engines which create all pages in one go and save them as HTML pages wh Ich are then served to the users. But this'll scenarios where the page content is the same for all requests [user-independent] as in the Seco nd example above. The listing of the same irrespective of the user requesting the page. Even if there is provision to listing books category Wise by providing different category ID values through the Querystri Ng, the page output for a particular category of the "the same for all users."
asp.net provides support for "caching" which'll help us solve this problem to a great extend. It can cache [store in memory] the output generated by a page and would serve this cached content for future requests. And this is useful the second scenario described earlier, where the page content was the same for all requests [use R-independent]. The caching feature is customizable in various ways and we'll have a we can do so as we go through this article.
Caching a page
In order to cache a page ' s output, we are need to specify an @OutputCache directive on the top of the page. The syntax is as shown below:
<%@ OutputCache duration=5 varybyparam= "None"%>
As you can, there are two attributes to this directive. They are:
Duration-the time in seconds of how long the output should is cached. After the specified duration has elapsed, the cached output would be removed with page content generated for the next reques T. That output would again be cached for the seconds and the process repeats.
Varybyparam-this is compulsory and specifies the querystring parameters to vary the cache.
In the above snippet, we have specified the VaryByParam attribute as None which means the page content to being served is the Same regardless of the parameters passed through the QueryString [click Example 1 in the sample download].
If There are two requests to the same page with varying querystring parameters, e.g.:/pagecachingbyparam.aspx?id=12 D./pagecachingbyparam.aspx?id=15] and separate page content is generated to each of them, the directive should to be:
<%@ OutputCache duration=5 varybyparam= "id"%>
The page content for the two requests'll each is cached for the time specified by the Duration attribute [to Example 2 In the sample download].
To specify multiple parameters, use semicolon to separate the parameter names. If we specify the VaryByParam as *, the cached content is varied for all parameters passed through the Querystri Ng.
Some pages generate different content for different browsers. In such cases, there are provision to vary the cached output for different browsers. The @OutputCache directive has to is modified to:
<%@ OutputCache duration=5 varybyparam= "id" varybycustom= "browser"%>
This would vary the cached output not only for the browser but also its major version. i.e., IE5, IE 6, Netscape 4, Netscape 6 'll all get different cached versions of the output.
Caching page Fragments
Sometimes we might want to cache just portions of a page. For example, we might have a header to our page which'll have the same content for all users. There might is some text/image in the header which might change everyday. In the case, we'll want to the cache this header for a duration of the a day.
The solution is to put the header contents into a user control and then specify this user control content should be CA Ched. This technique is called fragment caching.
To specify which a user control should is cached, we use the @OutputCache directive just like we used it for the page.
<%@ OutputCache duration=10 varybyparam= "None"%>
With the above directive, the user control content is cached for the time specified by the Duration attribute [ten sec S]. Regardless of the QueryString parameters and browser type and/or version, the same cached output is served. [Example 3 in the download for a demonstration].
Data Caching
ASP.net also supports caching of data as Object