Прочетете на английски Редактиране

Споделяне чрез


CacheItemPolicy Class

Definition

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

C#
public class CacheItemPolicy
Inheritance
CacheItemPolicy

Examples

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.

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;  
        }  

Remarks

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 Inheritors

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

Constructors

CacheItemPolicy()

Initializes a new instance of the CacheItemPolicy class.

Properties

AbsoluteExpiration

Gets or sets a value that indicates whether a cache entry should be evicted at a specified point in time.

ChangeMonitors

Gets a collection of ChangeMonitor objects that are associated with a cache entry.

Priority

Gets or sets a priority setting that is used to determine whether to evict a cache entry.

RemovedCallback

Gets or sets a reference to a CacheEntryRemovedCallback delegate that is called after an entry is removed from the cache.

SlidingExpiration

Gets 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.

UpdateCallback

Gets or sets a reference to a CacheEntryUpdateCallback delegate that is called before a cache entry is removed from the cache.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(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)

Applies to

Продукт Версии
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)