Export (0) Print
Expand All

CacheItemPolicy Class

Represents a set of eviction and expiration details for a specific cache entry.

System.Object
  System.Runtime.Caching.CacheItemPolicy

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

public class CacheItemPolicy

The CacheItemPolicy type exposes the following members.

  NameDescription
Public methodCacheItemPolicyInitializes a new instance of the CacheItemPolicy class.
Top

  NameDescription
Public propertyAbsoluteExpirationGets or sets a value that indicates whether a cache entry should be evicted after a specified duration.
Public propertyChangeMonitorsGets a collection of ChangeMonitor objects that are associated with a cache entry.
Public propertyPriorityGets or sets a priority setting that is used to determine whether to evict a cache entry.
Public propertyRemovedCallbackGets or sets a reference to a CacheEntryRemovedCallback delegate that is called after an entry is removed from the cache.
Public propertySlidingExpirationGets or sets a value that indicates whether a cache entry should be evicted if it has not been accessed in a given span of time.
Public propertyUpdateCallbackGets or sets a reference to a CacheEntryUpdateCallback delegate that is called before a cache entry is removed from the cache.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

A CacheItemPolicy instance contains information that can be associated with a cache entry. For example, when a cache entry is about to be removed from the cache, a CacheEntryUpdateArguments object is passed to a callback method. The UpdatedCacheItemPolicy property of the CacheEntryUpdateArguments object can pass a reference to a CacheItemPolicy instance that can include eviction and expiration details about the cache entry.

Some methods in the MemoryCache and ObjectCache classes accept a CacheItemPolicy instance to describe eviction or expiration policy.

Notes to Implementers

The CacheItemPolicy type is unsealed so that custom cache developers can extend it.

The following example shows how to create an in-memory cache item that monitors the path for a text file. The cache creates a CacheItemPolicy object and sets the AbsoluteExpiration property to evict the cache after 60 seconds.

[Visual Basic]

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 String = TryCast(cache("filecontents"), _
            String)
    If fileContents Is Nothing Then
        Dim policy As New CacheItemPolicy()
        policy.AbsoluteExpiration = _
            DateTimeOffset.Now.AddSeconds(60.0)
        Dim filePaths As New List(Of String)()
     Dim cachedFilePath As String = Server.MapPath("~") & _
         "\cacheText.txt"
        filePaths.Add(cachedFilePath)
        policy.ChangeMonitors.Add(New _
            HostFileChangeMonitor(filePaths))

        ' Fetch the file contents.
        fileContents = File.ReadAllText(cachedFilePath)
        cache.Set("filecontents", fileContents, policy)
    End If
    Label1.Text = fileContents
End Sub

[C#]

protected void Button1_Click(object sender, EventArgs e)
    {
        ObjectCache cache = MemoryCache.Default;
            string fileContents = cache["filecontents"] as string;
            if (fileContents == null)
            {
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = 
                    DateTimeOffset.Now.AddSeconds(60.0);
                
                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.
                fileContents = File.ReadAllText(cachedFilePath);
                
                cache.Set("filecontents", fileContents, policy);

            }

            Label1.Text = fileContents;
        }

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

Windows 8.1, Windows Server 2012 R2, 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.

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