Cache.Add(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) 方法


将指定项添加到 Cache 对象,该对象具有依赖项、到期和优先级策略以及一个委托(可用于在从 Cache 移除插入项时通知应用程序)。Adds the specified item to the Cache object with dependencies, expiration and priority policies, and a delegate you can use to notify your application when the inserted item is removed from the Cache.

 System::Object ^ Add(System::String ^ key, System::Object ^ value, System::Web::Caching::CacheDependency ^ dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System::Web::Caching::CacheItemPriority priority, System::Web::Caching::CacheItemRemovedCallback ^ onRemoveCallback);
public object Add (string key, object value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System.Web.Caching.CacheItemPriority priority, System.Web.Caching.CacheItemRemovedCallback onRemoveCallback);
member this.Add : string * obj * System.Web.Caching.CacheDependency * DateTime * TimeSpan * System.Web.Caching.CacheItemPriority * System.Web.Caching.CacheItemRemovedCallback -> obj
Public Function Add (key As String, value As Object, dependencies As CacheDependency, absoluteExpiration As DateTime, slidingExpiration As TimeSpan, priority As CacheItemPriority, onRemoveCallback As CacheItemRemovedCallback) As Object



用于引用该项的缓存键。The cache key used to reference the item.


要添加到缓存的项。The item to be added to the cache.


该项的文件依赖项或缓存键依赖项。The file or cache key dependencies for the item. 当任何依赖项更改时,该对象即无效,并从缓存中移除。When any dependency changes, the object becomes invalid and is removed from the cache. 如果没有依赖项,则此参数包含 nullIf there are no dependencies, this parameter contains null.


所添加对象将到期并被从缓存中移除的时间。The time at which the added object expires and is removed from the cache. 如果使用可调到期,则 absoluteExpiration 参数必须为 NoAbsoluteExpirationIf you are using sliding expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.


最后一次访问所添加对象时与该对象到期时之间的时间间隔。The interval between the time the added object was last accessed and the time at which that object expires. 如果该值等效于 20 分钟,则对象在最后一次被访问 20 分钟之后将到期并从缓存中移除。If this value is the equivalent of 20 minutes, the object expires and is removed from the cache 20 minutes after it is last accessed. 如果使用绝对到期,则 slidingExpiration 参数必须为 NoSlidingExpirationIf you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.


对象的相对成本,由 CacheItemPriority 枚举表示。The relative cost of the object, as expressed by the CacheItemPriority enumeration. 缓存在退出对象时使用该值;具有较低成本的对象在具有较高成本的对象之前被从缓存移除。The cache uses this value when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.


在从缓存中移除对象时所调用的委托(如果提供)。A delegate that, if provided, is called when an object is removed from the cache. 当从缓存中删除应用程序的对象时,可使用它来通知应用程序。You can use this to notify applications when their objects are deleted from the cache.


如果添加的项之前存储在缓存中,则为表示该项的对象;否则为 nullAn object that represents the item that was added if the item was previously stored in the cache; otherwise, null.


keyvalue 参数设置为 nullThe key or value parameter is set to null.

slidingExpiration 参数设置成小于 TimeSpan.Zero 或大于一年。The slidingExpiration parameter is set to less than TimeSpan.Zero or more than one year.

为要添加到 Cache 中的项设置 absoluteExpirationslidingExpiration 参数。The absoluteExpiration and slidingExpiration parameters are both set for the item you are trying to add to the Cache.


下面的示例创建一个AddItemToCache方法。The following example creates an AddItemToCache method. 调用此方法时, itemRemoved它将属性设置为, 并将onRemove方法注册到false CacheItemRemovedCallback委托的新实例。When this method is called, it sets an itemRemoved property to false and registers an onRemove method with a new instance of the CacheItemRemovedCallback delegate. 委托的签名用于RemovedCallback方法。The delegate's signature is used in the RemovedCallback method. 然后AddItemToCache , 方法检查与缓存中的Key1键相关联的值。The AddItemToCache method then checks the value associated with the Key1 key in the cache. 如果值为null, 则该Add方法将在Key1缓存中放置一个项, 其值为Value 1, 值为, 绝对过期时间为60秒, 缓存优先级较高。If the value is null, the Add method places an item in the cache with a key of Key1, a value of Value 1, an absolute expiration of 60 seconds, and a high cache priority. 它还使用onRemove方法作为参数。It also uses the onRemove method as an argument. 这允许RemovedCallback在从缓存中删除此项时调用方法。This allows the RemovedCallback method to be called when this item is removed from the cache.


有关如何使用CacheDependencyCacheItemRemovedCallback和委托的示例, 请参阅缓存应用程序数据For examples of how to use the CacheDependency class and the CacheItemRemovedCallback delegate, see Caching Application Data.

public void AddItemToCache(Object sender, EventArgs e) {
    itemRemoved = false;

    onRemove = new CacheItemRemovedCallback(this.RemovedCallback);

    if (Cache["Key1"] == null)
      Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
Public Sub AddItemToCache(sender As Object, e As EventArgs)
    itemRemoved = false

    onRemove = New CacheItemRemovedCallback(AddressOf Me.RemovedCallback)

    If (IsNothing(Cache("Key1"))) Then
      Cache.Add("Key1", "Value 1", Nothing, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove)
    End If
End Sub


如果具有相同key参数的项已存储Cache在中, 则对此方法的调用将自动失败。Calls to this method will fail silently if an item with the same key parameter is already stored in the Cache. 若要使用相同Cache key参数覆盖现有项, 请使用Insert方法。To overwrite an existing Cache item using the same key parameter, use the Insert method.

不能同时absoluteExpiration设置和slidingExpiration参数。You cannot set both the absoluteExpiration and slidingExpiration parameters. 如果希望缓存项在特定时间过期, 请将absoluteExpiration参数设置为特定时间, slidingExpiration并将参数设置为NoSlidingExpirationIf you intend the cache item to expire at a specific time, you set the absoluteExpiration parameter to the specific time, and the slidingExpiration parameter to NoSlidingExpiration.

如果希望缓存项在上次访问项后经过一定时间之后过期, 请将slidingExpiration参数设置为过期时间间隔, absoluteExpiration并将参数设置为NoAbsoluteExpirationIf you intend the cache item to expire after a certain amount of time has passed since the item was last accessed, you set the slidingExpiration parameter to the expiration interval, and the absoluteExpiration parameter to NoAbsoluteExpiration.