Gil Fink on .Net

Fink about IT

News

Microsoft MVP

MCPD Enterprise Applications Developer

Gil Fink

My Linkedin profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2010 Gil Fink

Blog Roll

Hebrew Articles

Index Pages

My OSS Projects

ASP.NET Output Cache Provider

ASP.NET Output Cache Provider

One of the newASP.NET Output Cache Provider
features that were
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 nothing 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 include 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 all we need to do is to inherit the OutputCacheProvider class.
The following code sample is a naive in-memory implementation for
OutputCacheProvider:

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 a in-memory dictionary.
The implementation is very simple and it is based on a item
class which looks like:

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 show you how to do that:

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

<%@ 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:

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

Summery

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.

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# July 18, 2010 2:20 PM

ASP.NET Output Cache Provider – Gil Fink on .Net - asp said:

Pingback from  ASP.NET Output Cache Provider &#8211; Gil Fink on .Net - asp

# July 19, 2010 2:45 AM

ASP.NET Output Cache Provider – Gil Fink on .Net « cache said:

Pingback from  ASP.NET Output Cache Provider &#8211; Gil Fink on .Net &laquo;  cache

# July 19, 2010 5:12 AM

Twitter Trackbacks for ASP.NET Output Cache Provider - Gil Fink on .Net [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 ASP.NET Output Cache Provider - Gil Fink on .Net         [microsoft.co.il]        on Topsy.com

# July 19, 2010 6:11 PM

Articles » Blog Archive » An Introduction to Factory Farming said:

Pingback from  Articles  &raquo; Blog Archive   &raquo; An Introduction to Factory Farming

# July 22, 2010 12:36 PM

Thanigainathan said:

Can we handle this programatically like we do in frameworks where we wont need to add the config entries ?

Thanks,

Thani

# July 30, 2010 10:05 AM

Gil Fink said:

@Thani,

Yes we can but currenlty only per request and in the Global.asax.

In the Global.asax you can override the GetOutputCacheProviderName method. The method porpuse is to return the provider name that the ASP.NET output cache will use.

# July 30, 2010 10:39 AM

Gil Fink on .Net said:

Changing The OutputCache Provider During Runtime I got a question in the post I wrote about ASP.NET Output

# August 1, 2010 7:58 AM

Paul said:

That mean we could also clear the cache in code?

# August 10, 2010 8:11 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: