Click here to Skip to main content
13,256,260 members (46,865 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 5 Nov 2010

Performance: Applying Output Cache Policies to a User Control

, 8 Nov 2010
Rate this:
Please Sign up or sign in to vote.
How to apply output cache policies to a user control

One of the great features in ASP.NET is the rich caching framework. I particularly like being able to:

  1. Define caching policies in the configuration file and then assigning those policies to groups of pages (via the @OutputCache directive).
  2. Cache User Control output for reuse on multiple pages (Partial Page Caching)

What I don’t like is the fact that you can’t combine the two! You have to hard code your user control cache directives either in the mark-up (as @OutputCache directive) or by applying an attribute (System.Web.UI.PartialCaching) to the code behind, both of which require at least the duration to be hardcoded.

This is a massive oversight, as best-practise ASP.NET dictates precompiling websites prior to production which means caching rules can’t be tweaked without frequent rebuild/deployment cycles.

Fortunately, we can work around the issue!

As with most of my workarounds, the trick is a new Extension Method to dynamically alter the UserControls Cache Policy:

 1: using System;
 2: using System.Linq;
 3: using System.Web.Configuration;
 4: using System.Web.UI;
 7: namespace MartinOnDotNet.Helpers.UI
 8: {
 9:     /// <summary>
10:     /// Useful extension methods for Controls
11:     /// </summary>
12:     public static class ControlExtensions
13:     {
16:         private static OutputCacheSettingsSection _outputCacheSettings = ((
18:         /// <summary>
19:         /// Applies the cache policy.
20:         /// </summary>
21:         /// <param name="control">The control.</param>
22:         /// <param name="cachePolicyName">Name of the cache policy.</param>
23:         public static void ApplyCachePolicy(this UserControl control,
                string cachePolicyName)
24:         {
25:             if (control == null) throw new ArgumentNullException("control");
26:             if (string.IsNullOrEmpty(cachePolicyName)) throw new ArgumentNullException(
27:             if (_outputCacheSettings == null) return;
28:             OutputCacheProfile profile =
29:                 .Where(p => string.Equals(p.Name, cachePolicyName,
30:                 .FirstOrDefault();
31:             if (profile == null || !profile.Enabled) return;
32:             control.CachePolicy.SetExpires(DateTime.Now.AddSeconds(profile.Duration));
33:             if (!string.Equals(profile.VaryByParam, "none",
                    StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(|
34:                 foreach (string key in profile.VaryByParam.Split(new char[]{';'},
35:                     control.CachePolicy.VaryByParams[key] = true;
36:             else
37:                 control.CachePolicy.VaryByParams.IgnoreParams = true;
38:             if (!string.IsNullOrEmpty(
                    profile.VaryByCustom)) control.CachePolicy.SetVaryByCustom(
39:             if (!string.IsNullOrEmpty(
                    profile.VaryByControl)) control.CachePolicy.SetVaryByCustom(
42:         }
44:     }
45: }

To use it, each user control will need to have a line added to the OnInit override in the codebehind (*.ascx.cs):

 1: using System;
 2: using MartinOnDotNet.Helpers.UI;
 4: namespace MartinOnDotNet.Helpers.BrandingDemo
 5: {
 6:     /// <summary>
 7:     /// Example of a cached usercontrol
 8:     /// </summary>
 9:     public partial class CacheExample : System.Web.UI.UserControl
10:     {
11:         /// <summary>
12:         /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
13:         /// </summary>
14:         /// <param name="e">An
            /// <see cref="T:System.EventArgs"/> object that contains
            /// the event data.</param>
15:         protected override void OnInit(EventArgs e)
16:         {
17:             base.OnInit(e);
18:             if (this.CachePolicy.SupportsCaching)
19:                 this.ApplyCachePolicy("Test");
20:         }
22:     }
23: }

The ascx file will need a place holder @outputcache directive:

1: <%@ Control Language="C#"
AutoEventWireup="true" CodeBehind="CacheExample.ascx.cs"
2:     Inherits="MartinOnDotNet.Helpers.BrandingDemo.CacheExample" %>
4: <%@ OutputCache Duration="1"
VaryByParam="none" Shared="true" %>
6: <% = DateTime.Now.ToString("G") %>

The values in the directive act as defaults and will be overridden by parameters in the matching Cache Policy (Test, in the code above), apart from Shared which isn’t included in the config.

Finally, define your cache policy in the web.config file:

 1: <system.web>
 2:     <caching>
 3:         <outputCacheSettings>
 4:             <outputCacheProfiles>
 5:                 <add name="Test"
 6:                          duration="3600"
 7:                          varyByParam="none"
 8:             </outputCacheProfiles>
 9:         </outputCacheSettings>
10:     </caching>
11: </system.web>

The varyByCustom attribute in the rule above refers to some custom code in the Global.asax using the GetVaryByCustomString method. In this case, the code will vary by the current date in the given format making the cached version of the control rollover every minute. This is a bit wasteful, as the cache will hang on to each version of the control for 3600 seconds (an hour) but proves the principle works.

Happily, the outputCacheSettings element of the web.config file supports configSource so you can actually apply different profiles for different environments by using an external config file.

Happy caching!


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


About the Author

Martin Jarvis
Software Developer (Senior) Freestyle Interactive Ltd
United Kingdom United Kingdom
I'm a lead developer for Freestyle Interactive Ltd where we create many wonderful websites built on Microsofts ASP.Net and Ektron CMS.

I've been developing .Net applications (both Windows and Web) since 2002.

You may also be interested in...


Comments and Discussions

GeneralCacheDependency support Pin
JonathanSP21-Mar-11 15:49
memberJonathanSP21-Mar-11 15:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 8 Nov 2010
Article Copyright 2010 by Martin Jarvis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid