Cache.Insert Cache.Insert Cache.Insert Cache.Insert Method

定义

Cache 对象插入项。Inserts an item into the Cache object. 使用此方法的某一版本覆盖具有相同 key 参数的现有 Cache 项。Use one of the versions of this method to overwrite an existing Cache item with the same key parameter.

重载

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)

Cache 对象中插入对象,后者具有依赖项、到期和优先级策略以及一个委托(可用于在从 Cache 移除插入项时通知应用程序)。Inserts an object into 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.

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)

将对象与依赖项、到期策略以及可用于在从缓存中移除项之前通知应用程序的委托一起插入到 Cache 对象中。Inserts an object into the Cache object together with dependencies, expiration policies, and a delegate that you can use to notify the application before the item is removed from the cache.

Insert(String, Object, CacheDependency) Insert(String, Object, CacheDependency) Insert(String, Object, CacheDependency) Insert(String, Object, CacheDependency)

Cache 中插入具有文件依赖项或键依赖项的对象。Inserts an object into the Cache that has file or key dependencies.

Insert(String, Object) Insert(String, Object) Insert(String, Object) Insert(String, Object)

Cache 对象插入项,该项带有一个缓存键引用其位置,并使用 CacheItemPriority 枚举提供的默认值。Inserts an item into the Cache object with a cache key to reference its location, using default values provided by the CacheItemPriority enumeration.

Insert(String, Object, CacheDependency, DateTime, TimeSpan) Insert(String, Object, CacheDependency, DateTime, TimeSpan) Insert(String, Object, CacheDependency, DateTime, TimeSpan) Insert(String, Object, CacheDependency, DateTime, TimeSpan)

Cache 中插入具有依赖项和到期策略的对象。Inserts an object into the Cache with dependencies and expiration policies.

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)

Cache 对象中插入对象,后者具有依赖项、到期和优先级策略以及一个委托(可用于在从 Cache 移除插入项时通知应用程序)。Inserts an object into 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.

public:
 void Insert(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 void Insert (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.Insert : string * obj * System.Web.Caching.CacheDependency * DateTime * TimeSpan * System.Web.Caching.CacheItemPriority * System.Web.Caching.CacheItemRemovedCallback -> unit
Public Sub Insert (key As String, value As Object, dependencies As CacheDependency, absoluteExpiration As DateTime, slidingExpiration As TimeSpan, priority As CacheItemPriority, onRemoveCallback As CacheItemRemovedCallback)

参数

key
String String String String

用于引用该对象的缓存键。The cache key used to reference the object.

value
Object Object Object Object

要插入缓存中的对象。The object to be inserted in the cache.

dependencies
CacheDependency CacheDependency CacheDependency CacheDependency

该项的文件依赖项或缓存键依赖项。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.

absoluteExpiration
DateTime DateTime DateTime DateTime

所插入对象将到期并被从缓存中移除的时间。The time at which the inserted object expires and is removed from the cache. 要避免可能的本地时间问题(例如从标准时间改为夏时制),请使用 UtcNow 而不是 Now 作为此参数值。To avoid possible issues with local time such as changes from standard time to daylight saving time, use UtcNow rather than Now for this parameter value. 如果使用绝对到期,则 slidingExpiration 参数必须为 NoSlidingExpirationIf you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.

slidingExpiration
TimeSpan TimeSpan TimeSpan TimeSpan

最后一次访问所插入对象时与该对象到期时之间的时间间隔。The interval between the time the inserted 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 will expire and be removed from the cache 20 minutes after it was last accessed. 如果使用可调到期,则 absoluteExpiration 参数必须为 NoAbsoluteExpirationIf you are using sliding expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.

priority
CacheItemPriority CacheItemPriority CacheItemPriority CacheItemPriority

该对象相对于缓存中存储的其他项的成本,由 CacheItemPriority 枚举表示。The cost of the object relative to other items stored in the cache, as expressed by the CacheItemPriority enumeration. 该值由缓存在退出对象时使用;具有较低成本的对象在具有较高成本的对象之前被从缓存移除。This value is used by the cache when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.

onRemoveCallback
CacheItemRemovedCallback CacheItemRemovedCallback CacheItemRemovedCallback CacheItemRemovedCallback

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

异常

keyvalue 参数为 nullThe key or value parameter is null.

slidingExpiration 参数设置为小于 TimeSpan.Zero 或大于一年的等效值。You set the slidingExpiration parameter to less than TimeSpan.Zero or the equivalent of 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.

示例

下面的示例演示如何在将某个项插入到应用程序的Cache对象中时将该项分配高优先级。The following example demonstrates how to assign an item high priority when you insert it into your application's Cache object.

备注

有关如何将此方法与CacheItemRemovedCallback委托一起使用的详细信息, 请参阅如何:从缓存中删除项时通知应用程序。For more information about how to use this method with the CacheItemRemovedCallback delegate, see How to: Notify an Application When an Item Is Removed from the Cache.

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero, CacheItemPriority.High, onRemove);
   
Cache.Insert("DSN", connectionString, Nothing, DateTime.Now.AddMinutes(2), TimeSpan.Zero, CacheItemPriority.High, onRemove)

注解

此方法将覆盖具有相同Cache key参数的现有项。This method will overwrite an existing Cache item with the same key parameter.

不能同时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 last access to the item, you set the slidingExpiration parameter to the expiration interval, and the absoluteExpiration parameter to NoAbsoluteExpiration.

另请参阅

Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback) Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemUpdateCallback)

将对象与依赖项、到期策略以及可用于在从缓存中移除项之前通知应用程序的委托一起插入到 Cache 对象中。Inserts an object into the Cache object together with dependencies, expiration policies, and a delegate that you can use to notify the application before the item is removed from the cache.

public:
 void Insert(System::String ^ key, System::Object ^ value, System::Web::Caching::CacheDependency ^ dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System::Web::Caching::CacheItemUpdateCallback ^ onUpdateCallback);
public void Insert (string key, object value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, System.Web.Caching.CacheItemUpdateCallback onUpdateCallback);
member this.Insert : string * obj * System.Web.Caching.CacheDependency * DateTime * TimeSpan * System.Web.Caching.CacheItemUpdateCallback -> unit
Public Sub Insert (key As String, value As Object, dependencies As CacheDependency, absoluteExpiration As DateTime, slidingExpiration As TimeSpan, onUpdateCallback As CacheItemUpdateCallback)

参数

key
String String String String

用于引用对象的缓存键。The cache key that is used to reference the object.

value
Object Object Object Object

要插入到缓存中的对象。The object to insert into the cache.

dependencies
CacheDependency CacheDependency CacheDependency CacheDependency

该项的文件依赖项或缓存键依赖项。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.

absoluteExpiration
DateTime DateTime DateTime DateTime

所插入对象将到期并被从缓存中移除的时间。The time at which the inserted object expires and is removed from the cache. 要避免可能的本地时间问题(例如从标准时间改为夏时制),请使用 UtcNow 而不是 Now 作为此参数值。To avoid possible issues with local time such as changes from standard time to daylight saving time, use UtcNow instead of Now for this parameter value. 如果使用绝对到期,则 slidingExpiration 参数必须设置为 NoSlidingExpirationIf you are using absolute expiration, the slidingExpiration parameter must be set to NoSlidingExpiration.

slidingExpiration
TimeSpan TimeSpan TimeSpan TimeSpan

缓存对象的上次访问时间和对象的到期时间之间的时间间隔。The interval between the time that the cached 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 will expire and be removed from the cache 20 minutes after it was last accessed. 如果使用可调到期,则 absoluteExpiration 参数必须设置为 NoAbsoluteExpirationIf you are using sliding expiration, the absoluteExpiration parameter must be set to NoAbsoluteExpiration.

onUpdateCallback
CacheItemUpdateCallback CacheItemUpdateCallback CacheItemUpdateCallback CacheItemUpdateCallback

从缓存中移除对象之前将调用的委托。A delegate that will be called before the object is removed from the cache. 可以使用它来更新缓存项并确保缓存项不会从缓存中移除。You can use this to update the cached item and ensure that it is not removed from the cache.

异常

keyvalueonUpdateCallback 参数为 nullThe key, value, or onUpdateCallback parameter is null.

slidingExpiration 参数设置为小于 TimeSpan.Zero 或大于一年的等效值。You set the slidingExpiration parameter to less than TimeSpan.Zero or the equivalent of 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.

-or- dependencies 参数为 nullabsoluteExpiration 参数设置为 NoAbsoluteExpiration 并且 slidingExpiration 参数设置为 NoSlidingExpirationThe dependencies parameter is null, and the absoluteExpiration parameter is set to NoAbsoluteExpiration, and the slidingExpiration parameter is set to NoSlidingExpiration.

注解

Insert方法使你能够在缓存中插入新项, 并提供一个在从缓存中移除该项之前调用的委托。The Insert method enables you to insert a new item in the cache and provide a delegate that is called before the item is removed from the cache. 在委托中, 可以更新缓存的项, 从而防止将其从缓存中删除。In the delegate, you can update the cached item and thereby prevent it from being removed from the cache.

此方法将覆盖具有相同Cache key参数的现有项。This method will overwrite an existing Cache item with the same key parameter.

不能同时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 set period of time since the item was last accessed, you set the slidingExpiration parameter to the expiration interval, and you set the absoluteExpiration parameter to NoAbsoluteExpiration.

另请参阅

Insert(String, Object, CacheDependency) Insert(String, Object, CacheDependency) Insert(String, Object, CacheDependency) Insert(String, Object, CacheDependency)

Cache 中插入具有文件依赖项或键依赖项的对象。Inserts an object into the Cache that has file or key dependencies.

public:
 void Insert(System::String ^ key, System::Object ^ value, System::Web::Caching::CacheDependency ^ dependencies);
public void Insert (string key, object value, System.Web.Caching.CacheDependency dependencies);
member this.Insert : string * obj * System.Web.Caching.CacheDependency -> unit
Public Sub Insert (key As String, value As Object, dependencies As CacheDependency)

参数

key
String String String String

用于标识该项的缓存键。The cache key used to identify the item.

value
Object Object Object Object

要插入缓存中的对象。The object to be inserted in the cache.

dependencies
CacheDependency CacheDependency CacheDependency CacheDependency

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

异常

keyvalue 参数为 nullThe key or value parameter is null.

示例

下面的示例演示如何将一个项插入到应用程序的缓存中, 其中的缓存依赖于 XML 配置文件。The following example demonstrates how to insert an item into an application's cache with a cache dependency on an XML configuration file.

Cache.Insert("DSN", connectionString, new CacheDependency(Server.MapPath("myconfig.xml")));
   
Cache.Insert("DSN", connectionString, New CacheDependency(Server.MapPath("myconfig.xml")))

注解

此方法将覆盖其密钥与key参数匹配的现有缓存项。This method will overwrite an existing cache item whose key matches the key parameter. 使用此Insert方法的此重载添加到缓存中的对象不具有任何文件或缓存依赖项、 Default优先级、的可调过期值NoSlidingExpiration和绝对过期值NoAbsoluteExpirationThe object added to the cache using this overload of the Insert method is inserted with no file or cache dependencies, a priority of Default, a sliding expiration value of NoSlidingExpiration, and an absolute expiration value of NoAbsoluteExpiration.

另请参阅

Insert(String, Object) Insert(String, Object) Insert(String, Object) Insert(String, Object)

Cache 对象插入项,该项带有一个缓存键引用其位置,并使用 CacheItemPriority 枚举提供的默认值。Inserts an item into the Cache object with a cache key to reference its location, using default values provided by the CacheItemPriority enumeration.

public:
 void Insert(System::String ^ key, System::Object ^ value);
public void Insert (string key, object value);
member this.Insert : string * obj -> unit
Public Sub Insert (key As String, value As Object)

参数

key
String String String String

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

value
Object Object Object Object

要插入缓存中的对象。The object to be inserted into the cache.

异常

keyvalue 参数为 nullThe key or value parameter is null.

示例

下面的示例演示如何将一个项插入到应用程序的缓存中。The following example demonstrates how to insert an item into an application's cache.

Cache.Insert("DSN", connectionString);
   
Cache.Insert("DSN", connectionString)

注解

此方法将覆盖其密钥与key参数匹配的现有缓存项。This method will overwrite an existing cache item whose key matches the key parameter. 使用此Insert方法的此重载添加到缓存中的对象不具有任何文件或缓存依赖项、 Default优先级、的可调过期值NoSlidingExpiration和绝对过期值NoAbsoluteExpirationThe object added to the cache using this overload of the Insert method is inserted with no file or cache dependencies, a priority of Default, a sliding expiration value of NoSlidingExpiration, and an absolute expiration value of NoAbsoluteExpiration.

另请参阅

Insert(String, Object, CacheDependency, DateTime, TimeSpan) Insert(String, Object, CacheDependency, DateTime, TimeSpan) Insert(String, Object, CacheDependency, DateTime, TimeSpan) Insert(String, Object, CacheDependency, DateTime, TimeSpan)

Cache 中插入具有依赖项和到期策略的对象。Inserts an object into the Cache with dependencies and expiration policies.

public:
 void Insert(System::String ^ key, System::Object ^ value, System::Web::Caching::CacheDependency ^ dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);
public void Insert (string key, object value, System.Web.Caching.CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);
member this.Insert : string * obj * System.Web.Caching.CacheDependency * DateTime * TimeSpan -> unit
Public Sub Insert (key As String, value As Object, dependencies As CacheDependency, absoluteExpiration As DateTime, slidingExpiration As TimeSpan)

参数

key
String String String String

用于引用该对象的缓存键。The cache key used to reference the object.

value
Object Object Object Object

要插入缓存中的对象。The object to be inserted in the cache.

dependencies
CacheDependency CacheDependency CacheDependency CacheDependency

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

absoluteExpiration
DateTime DateTime DateTime DateTime

所插入对象将到期并被从缓存中移除的时间。The time at which the inserted object expires and is removed from the cache. 要避免可能的本地时间问题(例如从标准时间改为夏时制),请使用 UtcNow 而不是 Now 作为此参数值。To avoid possible issues with local time such as changes from standard time to daylight saving time, use UtcNow rather than Now for this parameter value. 如果使用绝对到期,则 slidingExpiration 参数必须为 NoSlidingExpirationIf you are using absolute expiration, the slidingExpiration parameter must be NoSlidingExpiration.

slidingExpiration
TimeSpan TimeSpan TimeSpan TimeSpan

上次访问插入对象的时间与对象过期时间之间的间隔。The interval between the time the inserted object is last accessed and the time at which that object expires. 如果此值为 20 分钟的等效值,此对象将过期并将在上次访问后 20 分钟从缓存中删除。If this value is the equivalent of 20 minutes, the object will expire and be removed from the cache 20 minutes after it was last accessed. 如果使用可调到期,则 absoluteExpiration 参数必须为 NoAbsoluteExpirationIf you are using sliding expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.

异常

keyvalue 参数为 nullThe key or value parameter is null.

slidingExpiration 参数设置为小于 TimeSpan.Zero 或大于一年的等效值。You set the slidingExpiration parameter to less than TimeSpan.Zero or the equivalent of 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.

示例

下面的示例演示如何将一个项插入到应用程序的缓存中且具有绝对过期。The following example demonstrates how to insert an item into an application's cache with an absolute expiration.

备注

由于这种形式的Insert方法支持缓存依赖项, 因此, 如果项没有依赖项, 则必须在以null逗号Nothing分隔的参数列表中将dependency参数声明为 (在 Visual Basic 中)。Since this form of the Insert method supports cache dependencies, if the item has no dependencies, you must declare the dependency parameter as null (Nothing in Visual Basic) in the comma-delimited list of parameters.

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);
Cache.Insert("DSN", connectionString, Nothing, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration)

下面的示例演示如何使用可调过期将项插入缓存中。The following example demonstrates how to insert an item into the cache with a sliding expiration.

Cache.Insert("DSN", connectionString, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));
Cache.Insert("DSN", connectionString, Nothing, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10))

注解

此方法将覆盖具有相同Cache key参数的现有项。This method will overwrite an existing Cache item with the same key parameter.

如果参数设置为NoSlidingExpiration, 则将禁用可调过期。 slidingExpirationIf the slidingExpiration parameter is set to NoSlidingExpiration, sliding expiration is disabled. slidingExpiration如果将slidingExpiration参数设置为Zero"大于", 则absoluteExpiration参数将设置为Now加参数中包含的值。If you set the slidingExpiration parameter to greater than Zero, the absoluteExpiration parameter is set to Now plus the value contained in the slidingExpiration parameter. 如果在absoluteExpiration参数所指定的时间段之前从缓存中请求项, 则该项将再次放置在缓存中, 并absoluteExpiration将再次设置为DateTime.Now加上该slidingExpiration参数中包含的值。If the item is requested from the cache before the amount of time specified by the absoluteExpiration parameter, the item will be placed in the cache again, and absoluteExpiration will again be set to DateTime.Now plus the value contained in the slidingExpiration parameter. 如果在absoluteExpiration参数中的日期之前不会从缓存中请求该项, 则从缓存中删除该项。If the item is not requested from the cache before the date in the absoluteExpiration parameter, the item is removed from the cache. .. 使用 insert 方法的此重载添加到缓存的项将插入优先级为的DefaultThe item added to the cache using this overload of the insert method is inserted with a priority of Default.

另请参阅

适用于