Cache = Page.Cache

Returns an instance of the Cache class, which represents the cache for an application domain. Using the Cache property, data can be added to and retrieved from the cache.



A variable of type Cache that will receive the Cache instance.


The following code example adds two name/value pairs to the Cache object using the Cache property of the Page class and displays the values in a label control using the Page object’s Cache property:

Sub Page_Load(o As Object, e As EventArgs)
   Cache("Name") = "John Doe"
   Cache("Age") = 42
   Message.Text = CStr(Cache.Item("Name")) & " is " & _
      CStr(Cache("Age")) & " years old."
End Sub


Like the Application object, the Cache object is more commonly accessed directly through the Cache property, rather than by obtaining a local object reference to the Cache instance for the application.

Chapter 13 discusses when you might use the ASP.NET Cache rather than the Application state collection, and vice-versa.

The Cache class includes the following members:

Cache member


Add method

Adds an item to the cache

Count property

Indicates the number of items contained in the cache

Get method

Returns an object representing data in the cache with a particular key value

Insert method

Inserts an item into the cache and assigns it a key

Item property

Returns an object representing a cache item based on its key value or sets an item of data in the cache while assigning it a key value ...

Get ASP.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.