Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ASP.NET Output Cache Provider

DZone's Guide to

ASP.NET Output Cache Provider

· Performance Zone ·
Free Resource

Maintain Application Performance with real-time monitoring and instrumentation for any application. Learn More!

One of the new 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 an 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();
}
}

 

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.

Collect, analyze, and visualize performance data from mobile to mainframe with AutoPilot APM. Learn More!

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}