Cache Class

Implements the cache for a Web application. This class cannot be inherited.

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

'Declaration
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class Cache _
	Implements IEnumerable
'Usage
Dim instance As Cache

One instance of this class is created per application domain, and it remains valid as long as the application domain remains active. Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object.

NoteNote:

The Cache class is not intended for use outside of ASP.NET applications. It was designed and tested for use in ASP.NET to provide caching for Web applications. In other types of applications, such as console applications or Windows Forms applications, ASP.NET caching might not work correctly.

TopicLocation
How to: Delete Items from the Cache in ASP.NETBuilding ASP .NET Web Applications
How to: Add Items to the CacheBuilding ASP .NET Web Applications
How to: Delete Items from the Cache in ASP.NETBuilding ASP .NET Web Applications
How to: Add Items to the CacheBuilding ASP .NET Web Applications

The following example is a page that shows users the value assigned to an item in the cache, and then notifies them when the item is removed from the cache. It creates a RemovedCallback method, which has the signature of the CacheItemRemovedCallback delegate, to notify users when the cache item is removed, and it uses the CacheItemRemovedReason enumeration to tell them why it was removed. In addition, it uses the Cache.Item property to add objects to the cache and retrieve the value of those objects. In the AddItemToCache method, it uses the Cache.Add method to add an item to the cache. To use the CacheItemRemovedCallback delegate, you must add an item to the cache with this method or the Cache.Insert method, so that that ASP.NET can automatically make the proper method calls when the item is removed. The custom RemoveItemFromCache method uses the Cache.Remove method to explicitly delete the item from the cache, causing the RemovedCallback method to be invoked.

<%@ Page Language="VB" %>

<html>
 <Script runat=server>
    Shared itemRemoved As boolean = false
    Shared reason As CacheItemRemovedReason
    Dim onRemove As CacheItemRemovedCallback

    Public Sub RemovedCallback(k As String, v As Object, r As CacheItemRemovedReason)
      itemRemoved = true
      reason = r
    End Sub 

    Public Sub AddItemToCache(sender As Object, e As EventArgs)
        itemRemoved = false

        onRemove = New CacheItemRemovedCallback(AddressOf Me.RemovedCallback)

        If (IsNothing(Cache("Key1"))) Then
          Cache.Add("Key1", "Value 1", Nothing, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove)
        End If 
    End Sub 

    Public Sub RemoveItemFromCache(sender As Object, e As EventArgs)
        If (Not IsNothing(Cache("Key1"))) Then
          Cache.Remove("Key1")
        End If 
    End Sub
 </Script>

 <body>
  <Form runat="server">
    <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
    <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
  </Form>
<%
If (itemRemoved) Then
    Response.Write("RemovedCallback event raised.")
    Response.Write("<BR>")
    Response.Write("Reason: <B>" + reason.ToString() + "</B>")
Else
    Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"),String)) + "</B>")
End If
%>
 </body>
</html>

System.Object
  System.Web.Caching.Cache

This type is 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, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft