CacheItemPolicy 类

定义

表示指定缓存项的一组逐出和过期详细信息。

public ref class CacheItemPolicy
public class CacheItemPolicy
type CacheItemPolicy = class
Public Class CacheItemPolicy
继承
CacheItemPolicy

示例

下面的示例演示如何创建内存中缓存项来监视文本文件的路径。 缓存会创建一个 CacheItemPolicy 对象,并将 AbsoluteExpiration 属性设置为在60秒后逐出缓存。

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

注解

CacheItemPolicy实例包含可与某个缓存项关联的信息。 例如,当要从缓存中移除某个缓存项时, CacheEntryUpdateArguments 对象将被传递给回调方法。 UpdatedCacheItemPolicy对象的属性 CacheEntryUpdateArguments 可以传递对 CacheItemPolicy 实例的引用,该实例可以包含有关缓存项的逐出和过期详细信息。

和类中的某些方法 MemoryCache ObjectCache 接受 CacheItemPolicy 用于描述逐出或过期策略的实例。

继承者说明

CacheItemPolicy 类型是未密封的,以便自定义缓存开发人员可以对其进行扩展。

构造函数

CacheItemPolicy()

初始化 CacheItemPolicy 类的新实例。

属性

AbsoluteExpiration

获取或设置一个值,该值指示是否应在指定的时间点逐出缓存项。

ChangeMonitors

获取与某个缓存项关联的 ChangeMonitor 对象的集合。

Priority

获取或设置用于确定是否逐出某个缓存项的优先级别设置。

RemovedCallback

获取或设置对 CacheEntryRemovedCallback 委托的引用,在从缓存中移除某个项后将调用该委托。

SlidingExpiration

获取或设置一个值,该值指示如果某个缓存项在给定时段内未被访问,是否应被逐出。

UpdateCallback

获取或设置对 CacheEntryUpdateCallback 委托的引用,在从缓存中移除某个缓存项之前将调用该委托。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于