Output Caching based on File Dependency

In this article we will see how we can add output caching to our aspx pages and invalidate the cache based on change of a flat file like xml, txt etc.,

Basically, caching serves the purpose for serving repeated request from the cache such that the number of requests to the Server and the time taken can be drastically reduced. Say, there are about 100 requests for a page in regular intervals. At that time caching would be very useful since it will serve all the requests from the cache and reducing the load for the server.

In our aspx pages, we can enable caching simply by including the following tag in the top of the page.

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

The Duration specifies the number of seconds the cache will be valid. After that duration, the cache will expire.

Supposing we have some data file like xml or text file from which we populate the aspx page. Then, if we want to invalidate the cache as soon as the file is edited, it can be accomplished by using the FileDependency Method.

Response.AddFileDependency(Server.MapPath(physical root path of the file))

The above line of code needs to reside in the codebehind of the aspx page or as inline script within the page.

This would ensure that the page is not served from cache if the dependant file has been changed.

We will look into more dependencies such as Database etc., in forthcoming articles.

Print | posted on Monday, April 25, 2005 7:52 AM

Comments on this post

# re: Output Caching based on File Dependency

Requesting Gravatar...
this godd thanks ..
Left by mario oyunları on Sep 22, 2009 12:09 AM

Your comment:

 (will show your gravatar)