.NET Framework Class Library  

Cache Class

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

For a list of all members of this type, see Cache Members.

System.Object
   System.Web.Caching.Cache

[Visual Basic]
NotInheritable Public Class Cache
   Implements IEnumerable
[C#]
public sealed class Cache : IEnumerable
[C++]
public __gc __sealed class Cache : public IEnumerable
[JScript]
public class Cache implements IEnumerable

Thread Safety

This type is safe for multithreaded operations.

Remarks

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.

Example

[Visual Basic, C#, JScript] The following example is a page that shows users the value assigned to an item in the cache, then notifies them when the item is removed from the cache. It creates a RemovedCallback method, which uses the signature of the CacheItemRemovedCallback delegate, to notify users when the cache item is removed and 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.

[Visual Basic] 
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO"%> 

<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), TimeSpan.Zero, 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>" + Cache("Key1") + "</B>")
End If
%>
 </body>
</html>

[C#] 
<%@ Import Namespace="System.IO"%> 
<html>
 <Script runat=server language="C#">
    static bool itemRemoved = false;
    static CacheItemRemovedReason reason;
    CacheItemRemovedCallback onRemove = null;

    public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
      itemRemoved = true;
      reason = r;
    }

    public void AddItemToCache(Object sender, EventArgs e) {
        itemRemoved = false;

        onRemove = new CacheItemRemovedCallback(this.RemovedCallback);

        if (Cache["Key1"] == null)
          Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, onRemove);
    }

    public void RemoveItemFromCache(Object sender, EventArgs e) {
        if(Cache["Key1"] != null)
          Cache.Remove("Key1");
    }
 </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) {
        Response.Write("RemovedCallback event raised.");
        Response.Write("<BR>");
        Response.Write("Reason: <B>" + reason.ToString() + "</B>");
     }
     else {
        Response.Write("Value of cache key: <B>" + Cache["Key1"] + "</B>");
     }
  %>
 </body>
</html>

[JScript] 
<%@ Import Namespace="System.IO"%>
 
<html>
 <Script runat=server language="JScript">

    static var itemRemoved : boolean = false;
    static var reason : CacheItemRemovedReason;
    var onRemove : CacheItemRemovedCallback = null;

    public function RemovedCallback(k : String, v : Object, r : CacheItemRemovedReason){
      itemRemoved = true;
      reason = r;
    }

    public function AddItemToCache(sender : Object, e : EventArgs) {
        itemRemoved = false;

        onRemove = this.RemovedCallback;

        if (Cache["Key1"] == null)
          Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(10), TimeSpan.Zero, CacheItemPriority.High, onRemove);
    }

    public function RemoveItemFromCache(sender : Object, e : EventArgs) {
        if(Cache["Key1"] != null)
          Cache.Remove("Key1");
    }
 </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) {
    Response.Write("RemovedCallback event raised.");
    Response.Write("<BR>");
    Response.Write("Reason: <B>" + reason + "</B>");
}
else {
    Response.Write("Value of cache key: <B>" + Cache["Key1"] + "</B>");
}
%>

 </body>
</html>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.Caching

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

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

See Also

Cache Members | System.Web.Caching Namespace | HttpContext | CacheDependency | CacheItemRemovedCallback | Caching Application Data