代码改变世界

Cache.Insert 方法(摘自MSDN)

2013-04-11 13:20  Spring.Guo  阅读(944)  评论(0编辑  收藏  举报

Cache 类

继承层次结构

System.Object
System.Web.Caching.Cache

命名空间: System.Web.Caching
程序集: System.Web(在 System.Web.dll 中)

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

 

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

字段说明

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

公共字段静态成员
NoSlidingExpiration
用作 InsertAdd 方法调用中的 slidingExpiration 参数,以禁用可调到期。 此字段为只读。

 

代码示例:

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

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);
下面的示例演示如何向缓存中插入具有可调整到期的项。 (相对过期)
Cache.Insert("DSN", connectionString, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));
 
网页中,带依赖缓存:
它创建RemovedCallback 方法,该方法具有 CacheItemRemovedCallback 委托的签名,以在缓存项被移除时通知用户并使用 CacheItemRemovedReason 枚举告诉用户该项被移除的原因。
 此外,它使用 Cache.Item 属性将对象添加到缓存中并检索这些对象的值。 在 AddItemToCache 方法中,
它使用 Cache.Add 方法向缓存中添加项。 若要使用 CacheItemRemovedCallback 委托,您必须使用此方法或 Cache.Insert 方法向缓存中添加项,
以便该项被移除时 ASP.NET 能自动调用正确的方法。 自定义的 RemoveItemFromCache 方法使用Cache.Remove 方法显式地从缓存中删除该项,
这导致调用 RemovedCallback 方法。
 
<html>
 <Script runat=server language="C#">
    static bool itemRemoved = false;
    static CacheItemRemovedReason reason;
    CacheItemRemovedCallback onRemove = null;

    public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
      itemRemoved = true;
      reason = r;
    }

    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 void RemoveItemFromCache(Object sender, EventArgs e) {
        if(Cache["Key1"] != null)
          Cache.Remove("Key1");
    }
 </Script>
 <body>
  <Form runat="server">
   <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
   <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
  </Form>
  <% if (itemRemoved) {
        Response.Write("RemovedCallback event raised.");
        Response.Write("<BR>");
        Response.Write("Reason: <B>" + reason.ToString() + "</B>");
     }
     else {
        Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
     }
  %>
 </body>
</html>