This documentation is archived and is not being maintained.

CacheSection Class

Configures the global cache settings for an ASP.NET application. This class cannot be inherited.

Namespace:  System.Web.Configuration
Assembly:  System.Web (in System.Web.dll)

public sealed class CacheSection : ConfigurationSection

The CacheSection class provides a way to programmatically access and modify the <cache> section of a configuration file.

The ASP.NET caching feature is implemented by the Cache class. For more information, see ASP.NET Caching.


The CacheSection can write information into the related section of the configuration file according to the restrictions defined by the section property AllowDefinition whose value is MachineToApplication. Any attempt to write in a configuration file at a level not allowed in the hierarchy will result in an error message generated by the parser. However, you can use this class to read configuration information at any level in the hierarchy.

A cache is an application-specific hash table used to store frequently accessed data. Application and session state are similar to the cache, the application state being the most similar, due to its application-wide scope. One of the biggest differences between the cache and the application-state mechanism is that the cache supports dependencies These dependencies make it possible to build applications that automatically remove cached items when certain events occur.

The following code example shows a page and the related code file used to access the CacheSection section attributes.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ReadWriteCache.aspx.vb" Inherits="ReadWriteCache" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head id="Head1" runat="server">
    <title>Read Write Application Cache</title>
    <form id="form1" runat="server">
        <h2>Read Write Application Cache</h2>

        <asp:Label ID="Label1" Text="[Application Cache goes here.]" runat="server"></asp:Label>

        <hr />

        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Write Cache" /> &nbsp; &nbsp;
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Read Cache" />

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ReadWriteCache : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            Label1.Text = "Application Cache goes here.";


    protected void Button1_Click(object sender, EventArgs e)
        // Get the application configuration file.
        System.Configuration.Configuration config =

        System.Web.Configuration.CacheSection cacheSection =

        // Increase the PrivateBytesLimit property to 0.
        cacheSection.PrivateBytesLimit = 
            cacheSection.PrivateBytesLimit + 10;

        // Increase memory limit.
        cacheSection.PercentagePhysicalMemoryUsedLimit = 
            cacheSection.PercentagePhysicalMemoryUsedLimit + 1;

        // Increase poll time.
        cacheSection.PrivateBytesPollTime =
            cacheSection.PrivateBytesPollTime + TimeSpan.FromMinutes(1);

        // Enable or disable memory collection.
        cacheSection.DisableMemoryCollection = 

        // Enable or disable cache expiration.
        cacheSection.DisableExpiration =

        // Save the configuration file.


    protected void Button2_Click(object sender, EventArgs e)

        // Get the application configuration file.
        System.Configuration.Configuration config =

        System.Web.Configuration.CacheSection cacheSection =

        // Read the cache section.
        System.Text.StringBuilder buffer = new System.Text.StringBuilder();

        string currentFile = cacheSection.CurrentConfiguration.FilePath;
        bool dExpiration = cacheSection.DisableExpiration;
        bool dMemCollection = cacheSection.DisableMemoryCollection;
        TimeSpan pollTime = cacheSection.PrivateBytesPollTime;
        int phMemUse = cacheSection.PercentagePhysicalMemoryUsedLimit;
        long pvBytesLimit = cacheSection.PrivateBytesLimit;

        string cacheEntry = String.Format("File: {0} <br/>", currentFile);
        cacheEntry = String.Format("Expiration Disabled: {0} <br/>", dExpiration);
        cacheEntry = String.Format("Memory Collection Disabled: {0} <br/>", dMemCollection);
        cacheEntry = String.Format("Poll Time: {0} <br/>", pollTime.ToString());
        cacheEntry = String.Format("Memory Limit: {0} <br/>", phMemUse.ToString());
        cacheEntry = String.Format("Bytes Limit: {0} <br/>", pvBytesLimit.ToString());

        Label1.Text = buffer.ToString();

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0