Dot Net Perls

Cache Application Settings - ASP.NET

by Sam Allen

Problem

Retrieving application settings in ASP.NET is needlessly slow. Improve the performance of the appSettings in ASP.NET using site-wide caching. You want simplicity and and easy maintenance.

Solution: static classes 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>

Example: encapsulating the 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"];
    }
}

Example: accessing your 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;
}

Question: is this worthwhile?

Yes. It provides clearer code with little more work. If your site has thousands of visitors, performance is critical. Studies have shown that the faster pages go, the more ads people click on and the longer they stay.

Information: 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. [Class From Config - sourceforge.net]

Summary: appSettings and static classes in ASP.NET

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.

Dot Net Perls
About
Sitemap
ASP.NET
Cache Examples and Overview
Custom ASHX Handler Tutorial
Global Variables Example
HtmlTextWriter Use
Page_Load Event, AJAX Tutorial
New
Occurrence Count of String
StartsWith String Examples
© 2008 Sam Allen. All rights reserved.