博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cache.Insert 方法(摘自MSDN)
阅读量:6995 次
发布时间:2019-06-27

本文共 1898 字,大约阅读时间需要 6 分钟。

Cache 类

System.Web.Caching.Cache

命名空间:

程序集: System.Web(在 System.Web.dll 中)

 
public void Insert(	string key,	Object value,	CacheDependency dependencies,	DateTime absoluteExpiration,	TimeSpan slidingExpiration)

 

参数
key
类型:
<?XML:NAMESPACE PREFIX = [default] http://www.w3.org/1999/xhtml NS = "http://www.w3.org/1999/xhtml" />用于引用该对象的缓存键。
value
类型:
要插入缓存中的对象。
dependencies
类型:
所插入对象的文件依赖项或缓存键依赖项。 当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含 null。
absoluteExpiration
类型:
所插入对象将到期并被从缓存中移除的时间。 要避免可能的本地时间问题(例如从标准时间改为夏时制),请使用 而不是 作为此参数值。 如果使用绝对到期,则 slidingExpiration 参数必须为 。
slidingExpiration
类型:
最后一次访问所插入对象时与该对象到期时之间的时间间隔。 如果该值等效于 20 分钟,则对象在最后一次被访问 20 分钟之后将到期并被从缓存中移除。 如果使用可调到期,则 absoluteExpiration 参数必须为 。

字段说明

公共字段静态成员

<?XML:NAMESPACE PREFIX = [default] http://www.w3.org/1999/xhtml NS = "http://www.w3.org/1999/xhtml" />用于 方法调用中的 absoluteExpiration 参数中以指示项从不到期。 此字段为只读。

公共字段静态成员

用作 或 方法调用中的 slidingExpiration 参数,以禁用可调到期。 此字段为只读。

 

代码示例:

下面的示例演示如何向应用程序的缓存中插入具有绝对到期的项。(绝对过期)

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);
下面的示例演示如何向缓存中插入具有可调整到期的项。 (相对过期)
Cache.Insert("DSN", connectionString, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));
 
网页中,带依赖缓存:
它创建RemovedCallback 方法,该方法具有 CacheItemRemovedCallback 委托的签名,以在缓存项被移除时通知用户并使用  枚举告诉用户该项被移除的原因。
此外,它使用  属性将对象添加到缓存中并检索这些对象的值。 在 AddItemToCache 方法中,
它使用  方法向缓存中添加项。 若要使用 CacheItemRemovedCallback 委托,您必须使用此方法或  方法向缓存中添加项,
以便该项被移除时 ASP.NET 能自动调用正确的方法。 自定义的 RemoveItemFromCache 方法使用 方法显式地从缓存中删除该项,
这导致调用 RemovedCallback 方法。
 
    
<% if (itemRemoved) { Response.Write("RemovedCallback event raised."); Response.Write("
"); Response.Write("Reason: " + reason.ToString() + ""); } else { Response.Write("Value of cache key: " + Server.HtmlEncode(Cache["Key1"] as string) + ""); } %>

转载于:https://www.cnblogs.com/iampkm/archive/2013/04/11/3014306.html

你可能感兴趣的文章