Click here to Skip to main content
15,884,838 members
Articles / Web Development / ASP.NET

Expire page output cache for a specific query string parameter

Rate me:
Please Sign up or sign in to vote.
1.89/5 (4 votes)
2 Oct 2006CPOL1 min read 29.7K   11   1
How to invalidate output cached page for a specific parameter?

Introduction

This article demonstrates how we can remove a cached page for a specific query string parameter. This may be required if we don't want the complete page to be removed from the cache but only those instances of the page which we want.

For example, if our page name is product.aspx and we have configured the output cache for the page like:

ASP.NET
<%@ OutputCache duration ="600" varyByParam="ProducttId"%>

Now, we know this page will be cached depending on the value of the "ProductId" query string parameter. We call the page the first time with the following URL: http://somesite/product.aspx?productid=100. It will come from the server and not from the cache. The second call of the same URL will be serviced from the cache. If we make a call with this URL: http://somesite/product.aspx?productid=101, it will come from the server and not from the cache.

Now let's assume that we don't want our page to come from the cache for the productid 100, but want the page to come from cache for productid 101. For this, our query string will have "cache=off" if we want to expire the cache. So here, we require conditional cache expiration. For doing this, follow these steps:

Step 1: In the Page_Load event of the page, use the following code:

C#
string strCacheKey = HttpContext.Current.Request.Url.PathAndQuery.ToString(); 
int strLen;
strLen = strCacheKey.ToUpper().IndexOf("CACHE") - 1;
if (strLen > 0)
strCacheKey = strCacheKey.Substring(0, strCacheKey.ToUpper().IndexOf("CACHE") - 1);
HttpRuntime.Cache.Insert(strCacheKey, new object());
HttpContext.Current.Response.AddCacheItemDependency(strCacheKey);

Here we are adding a cache dependency for the current request, and the dependency will be an empty object.

Step 2: When cache=off is passed in the query string, we need to expire the cache.

So in the Global.asax Application_BeginRequest event, insert the following code:

C#
if (Request.QueryString["Cache"] != null)
{
    string resetCache = Request.QueryString["Cache"].ToString();
    if (resetCache.ToUpper() == "OFF")
    {
        //get cache key
        string strUrl = Request.Url.PathAndQuery.ToString();
        strUrl = strUrl.Substring(0, strUrl.ToUpper().IndexOf("CACHE") - 1);
        HttpRuntime.Cache.Remove(strUrl);
    }
}

And here you are. Now you can expire your output cached page for a specific productid, just by passing an extra query string parameter.

Enjoy!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Norway Norway
Growing up with the world wide web, witnessing many bubbles and the rise of information technology to becoming an indispensable part of human existence, I was intrigued and astonished by the drastic changes it brought to the way we lived then. I was drawn into this whirlpool of internet and technology as an enthusiast only to come out as a qualified software professional. Worked extensively on technologies, starting with Visual Basic, ASP, Oracle and MS SQL to WCF, .NET Core , Azure service fabric and Kubernetes. Architected applications for on-premises to hybrid clouds to cloud native environments.
Fortunately, I like what I do and work has been fulfilling.
It has been 20+ years of working for various organisations in several roles both technical and managerial but not a
dull day.

Comments and Discussions

 
GeneralThanks a lot vikram Pin
SusheilK7613-Jul-07 9:36
SusheilK7613-Jul-07 9:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.