CacheItem Class

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Represents an individual cache entry in the cache.

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


Public Class CacheItem


Initializes a new CacheItem instance using the specified key of a cache entry.

System_CAPS_pubmethodCacheItem(String, Object)

Initializes a new CacheItem instance using the specified key and a value of the cache entry.

System_CAPS_pubmethodCacheItem(String, Object, String)

Initializes a new CacheItem instance using the specified key, value, and region of the cache entry.


Gets or sets a unique identifier for a CacheItem instance.


Gets or sets the name of a region in the cache that contains a CacheItem entry.


Gets or sets the data for a CacheItem instance.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

The CacheItem class provides a logical representation of a cache entry, which can include regions by using the RegionName property. In the default ASP.NET cache implementation, a cache entry is a key/value pair.

Entries in the cache are not CacheItem instances. Instead, the cache provider can store cache entries in any internal format that is convenient. However, the cache API requires cache providers to be able to convert cache entries into CacheItem instances (and vice versa).

Custom cache implementations can inherit from the CacheItem class provide additional information about cache entries.

Notes to Implementers:

The ObjectCache class has methods that support adding, fetching, and updating cache data, and all these methods have overloads that support the CacheItem class. Therefore, a custom cache implementation can create an extended CacheItem class and use that extended class together with the existing ObjectCache API for cache entries.

The following example shows how to use the CacheItem class to store the contents of a file as a cache entry.

Imports System.Runtime.Caching
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim cache As ObjectCache = MemoryCache.Default

        Dim fileContents As CacheItem = cache.GetCacheItem("filecontents")

        If fileContents Is Nothing Then
            Dim policy As New CacheItemPolicy()

            Dim filePaths As New List(Of String)()

            Dim CachedFilePaths As String = Server.MapPath("~") & "\cacheText.txt"


            policy.ChangeMonitors.Add(New HostFileChangeMonitor(filePaths))

            ' Fetch the file contents 
            Dim fileData As String = File.ReadAllText(CachedFilePaths)

            fileContents = New CacheItem("filecontents", fileData)

            cache.Set(fileContents, policy)
        End If
        Label1.Text = TryCast(fileContents.Value, String)
    End Sub
End Class

.NET Framework
Available since 4.0

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

Return to top