CacheItem Class
Represents an individual cache entry in the cache.
Assembly: System.Runtime.Caching (in System.Runtime.Caching.dll)
The CacheItem type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | CacheItem(String) | Initializes a new CacheItem instance using the specified key of a cache entry. |
![]() | CacheItem(String, Object) | Initializes a new CacheItem instance using the specified key and a value of the cache entry. |
![]() | CacheItem(String, Object, String) | Initializes a new CacheItem instance using the specified key, value, and region of the cache entry. |
| Name | Description | |
|---|---|---|
![]() | Key | Gets or sets a unique identifier for a CacheItem instance. |
![]() | RegionName | Gets or sets the name of a region in the cache that contains a CacheItem entry. |
![]() | Value | Gets or sets the data for a CacheItem instance. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | 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 ImplementersThe 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.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Runtime.Caching; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { ObjectCache cache = MemoryCache.Default; CacheItem fileContents = cache.GetCacheItem("filecontents"); if (fileContents == null) { CacheItemPolicy policy = new CacheItemPolicy(); List<string> filePaths = new List<string>(); string cachedFilePath = Server.MapPath("~") + "\\cacheText.txt"; filePaths.Add(cachedFilePath); policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths)); // Fetch the file contents string fileData = File.ReadAllText(cachedFilePath); fileContents = new CacheItem("filecontents", fileData); cache.Set(fileContents, policy); } Label1.Text = (fileContents.Value as string); } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
