ASP.NET appSettings Cache

by Sam Allen - Updated January 8, 2010

You want to improve the performance of the appSettings in ASP.NET using site-wide caching. Retrieving application settings in ASP.NET is needlessly slow. Here we see how you can cache your appSettings values in the C# programming language, and why it is faster.

Using static caches in ASP.NET

Here is an optimization that not only makes the code 30 times faster, but also allows better code separation and clarity. We will use ASP.NET global variables. Here are the settings in Web.config.

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="SiteTitle" value="The New York Times - Breaking News..."/>
        <add key="SitePrefix" value=NYT/>
        <add key="SiteHeader" value="Breaking News, World News & Mutimedia"/>
        <add key="BaseUrl" value=http://nytimes.com//>
    </appSettings>

Encapsulating settings

Using properties is ideal for these appSettings. My first appSettings article demonstrates this, but the properties are slow because they cause repeated NameValueCollection lookups. We can make a new class in App_Code, and fill it as follows.

using System;
using System.Web;
using System.Web.Configuration;

public static class SiteGlobal
{
    /// <summary>
    /// Full site title tag at root.
    /// </summary>
    static public string RootTitle { get; set; }

    /// <summary>
    /// Header prefix on root page.
    /// </summary>
    static public string RootPrefix { get; set; }

    /// <summary>
    /// Header main part on root page.
    /// </summary>
    static public string RootHeader { get; set; }

    /// <summary>
    /// Main site Url with http://.
    /// </summary>
    static public string BaseUrl { get; set; }

    static SiteGlobal()
    {
        // Cache all these values in static properties.
        RootTitle = WebConfigurationManager.AppSettings["SiteTitle"];
        RootPrefix = WebConfigurationManager.AppSettings["SitePrefix"];
        RootHeader = WebConfigurationManager.AppSettings["SiteHeader"];
        BaseUrl = WebConfigurationManager.AppSettings["BaseUrl"];
    }
}

Using static classes. Static classes are useful for holding static methods and enforce code correctness. There is a static constructor that sets the properties. When the ASP.NET AppDomain for this site is initialized the properties will be accessed and cached. It has static public properties. Properties can be static just like variables and methods. These properties can be accessed without a class instance.

Accessing cached values

Now we can simply call into the SiteGlobal class and use the properties there for access to the appSettings. Using the cached values is 30 times faster, and will reduce memory pressure.

protected void Page_Load(object sender, EventArgs e)
{
    Title1.Text = SiteGlobal.RootTitle;
    Prefix1.InnerHtml = SiteGlobal.RootPrefix;
    Header1.InnerHtml = SiteGlobal.RootHeader;
}

Automating appSettings

This approach for caching application settings in a C# class has been worked on by other developers. One interesting open source project called Class From Config can do this automatically and is useful when there are lots of settings and making this change would be tedious.

(Visit sourceforge.net.)

Summary

Here we saw how you can cache appSettings in ASP.NET with a static class and static properties. This provides clearer code and also improves performance. This will cut milliseconds off of your page load times and, when averaged over thousands of visitors, will reduce your power costs.

(Do not copy this page.)

Dot Net Perls | Search
Caching | Cache Examples and Overview | Expiration Time and Cache | Output Caching Master Page | SetCacheability Issue | Windows File Cache Usage
C# | SaveFileDialog Tutorial | IntegralHeight Property (Windows Forms) | Array.FindIndex Method | File.Replace Method
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen