Changing The OutputCache Provider During Runtime

  • submit to reddit

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior .Net Consultant and Architect at Sela Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft. You can find his Blog here: http://www.gilfink.net Gil is a DZone MVB and is not an employee of DZone and has posted 92 posts at DZone. You can read more from them at their website. View Full User Profile

I got a question in the post I wrote about ASP.NET Output Cache Provider which asks whether we can change the cache provider during runtime. The post will try to answer the question.

Overriding The OutputCache Provider

One of the changes in ASP.NET 4 is the ability to override the OutputCache provider name in the Global.asax file. This enables us to create logic that stores the OutputCache in more then one type of cache according to some state or behavior. When you do that you need to supply the name of the OutputCache provider by overriding the GetOutputCacheProviderName method. For example, I register the OutputCache provider from the previous post in the web.config but I don’t indicate that it is my default provider:

<caching>

<outputCache>

<providers>

<add name="InMemory" type="InMemoryOutputCacheProvider"/>

</providers>

</outputCache>

</caching>

If I’ll run the application now the default OutputCache will be the ASP.NET supplied OutputCache. If I would like to override it then the following code which will be written in the Global.asax file will help me achieve it:


public class Global : System.Web.HttpApplication
{

public override string GetOutputCacheProviderName(HttpContext context)
{
return "InMemory";
}

protected void Application_Start(object sender, EventArgs e)
{

}

protected void Session_Start(object sender, EventArgs e)
{

}

protected void Application_BeginRequest(object sender, EventArgs e)
{

}

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{

}

protected void Application_Error(object sender, EventArgs e)
{

}

protected void Session_End(object sender, EventArgs e)
{

}

protected void Application_End(object sender, EventArgs e)
{

}
}

Pay attention that I return the provider name that was registered earlier in the configuration file. Also, I could have provided whatever behavior that I want in order to return my needed provider per some situation.

The scope of the GetOutputCacheProviderName method is per request which means that every request can get it’s own OutputCache provider.

Summary

Lets sum up, we can register more then one OutputCache provider to our application and decide according to some behavior when to use them. The way to achieve that is by overriding the GetOutputCacheProviderName method and returning the desired provider.

References
0

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)