Click here to Skip to main content
15,881,803 members
Articles / Web Development / HTML

ASP.NET Output Cache Provider

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 Jul 2010CPOL2 min read 18.8K  
One of the new features that was shipped with ASP.NET 4 was new providers for caching purpose. In this post, I’ll explain one of them – the OutputCacheProvider.

One of the new features that was shipped with ASP.NET 4 was new providers for caching purpose. In this post, I’ll explain one of them – the OutputCacheProvider.

OutputCacheProvider

Up until ASP.NET 4 the output cache mechanism was implemented as in-memory caching and we couldn’t do anything to change its behavior. If we desired to use a distributed cache like AppFabric caching or our own implementation, we couldn’t achieve it. From ASP.NET 4, the caching is now following the ASP.NET provider model which means that we can create our own provider in order to use caching. If we wish to use our own output cache, all we need to do is to inherit the OutputCacheProvider class which exists in the System.Web.Caching namespace. After we implement the relevant interface that includes the Add, Get, Remove and Set methods, we can plug our provider inside the web.config file in order to use it.

OutputCacheProvider Example

As I wrote earlier, all we need to do is to inherit the OutputCacheProvider class. The following code sample is a naive in-memory implementation for OutputCacheProvider:

C#
public class InMemoryOutputCacheProvider : OutputCacheProvider
{
  #region Members
 
  private Dictionary<string, InMemoryOutputCacheItem> _cache = 
      new Dictionary<string, InMemoryOutputCacheItem>();
  private readonly static object _syncLock = new object();
 
  #endregion
 
  #region Methods
 
  public override object Add(string key, object entry, DateTime utcExpiry)
  {
    Set(key, entry, utcExpiry);
    return entry;
  }
 
  public override object Get(string key)
  {
    InMemoryOutputCacheItem item = null;
    if (_cache.TryGetValue(key, out item))
    {
      if (item.UtcExpiry < DateTime.UtcNow)
      {
        Remove(key);
        return null;
      }
      return item.Value;
    }
    return null;
  }
 
  public override void Remove(string key)
  {
    InMemoryOutputCacheItem item = null;
    if (_cache.TryGetValue(key, out item))
    {
      _cache.Remove(key);
    }
  }
 
  public override void Set(string key, object entry, DateTime utcExpiry)
  {
    var item = new InMemoryOutputCacheItem(entry, utcExpiry);
    lock (_syncLock)
    {
      if (_cache.ContainsKey(key))
      {
        _cache[key] = item;
      }
      else
      {
        _cache.Add(key, item);
      }
    }
  }
 
  #endregion
}

In the code, I implement the cache as an in-memory dictionary. The implementation is very simple and it is based on an item class which looks like:

C#
public class InMemoryOutputCacheItem
{
  #region Members
 
  public DateTime UtcExpiry { get; set; }
  public object Value { get; set; }
 
  #endregion
 
  #region Ctor
 
  public InMemoryOutputCacheItem(object value, DateTime utcExpiry)
  {
    Value = value;
    UtcExpiry = utcExpiry;
  }
 
  #endregion
}

Configuring the OutputCacheProvider

In order to use the previous output cache implementation, we need to plug it into the web.config file of the application. Under system.web element, we need to add the caching element. Inside the caching element, we add an outputCache element and add the provider that we have created. The following example shows you how to do that:

XML
<?xml version="1.0"?>
<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
        <compilation debug="true" targetFramework="4.0">
        </compilation>
    <caching>
      <outputCache defaultProvider="InMemory">
        <providers>
          <add name="InMemory" type="InMemoryOutputCacheProvider"/>
        </providers>
      </outputCache>
    </caching>
        <authentication mode="Windows"/>
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
    </system.web>
</configuration>

Checking the Implementation

Now we are ready to go and test the implementation. The following web page contains a label control which shows the current date and time (which is inserted in the code behind). As you can see, I added the OutputCache page directive to the page in order to test the provider:

HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
    Inherits="HttpModuleTestWeb.WebForm1" %>
<%@ OutputCache Duration="15" VaryByParam="*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblTime" runat="server" />
    </div>
    </form>
</body>
</html>

The page code behind:

C#
public partial class WebForm1 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    lblTime.Text = DateTime.Now.ToString();
  }
}

Summary

In ASP.NET 4, the caching is now provider based. That means that we can plug our implementation to the cache including the output cache. In the post, I showed a simple example of how to replace the output cache with a naive in-memory implementation.

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 --