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

Changing the OutputCache Provider during Runtime

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
8 Aug 2010CPOL1 min read 11.5K   3  
How to Change the OutputCache Provider during Runtime

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.
This 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 than 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:

XML
<caching>
  <outputCache>
    <providers>
      <add name="InMemory" type="InMemoryOutputCacheProvider"/>
    </providers>
  </outputCache>
</caching>

If I 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:

C#
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 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 its own OutputCache provider.

Summary

To sum up, we can register more than 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.

This article was originally posted at http://feeds.feedburner.com/GilFinkBlog

License

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


Written By
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

Comments and Discussions

 
-- There are no messages in this forum --