Cache.Insert与Cache.Add
1. Cache.Insert("DSN", connectionString, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero, CacheItemPriority.High, onRemove);与
Cache.Add("DSN", connectionString, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero, CacheItemPriority.High, onRemove);
有什么区别!
2. 
我是在单独的一个类中使用的,必须要HttpContext context = HttpContext.Current;实例后,才能正常使用,是不是一定要这样?==============================顺便问一下:
其中
TimeSpan.Zero,DateTime.Now.AddMinutes(2)是什么意思

解决方案 »

  1.   

    Calls to this method will fail if an item with the same key parameter is already stored in the Cache. To overwrite an existing Cache item using the same key parameter, use the Insert method.用Add的话如果cache里有了相同的键(key),将失败(will fail,没说到有异常,应该就是加不进去)用Insert的话可以通过相同的键(key)覆盖以有的值(value)TimeSpan.Zero,DateTime.Now.AddMinutes(2)是什么意思
    ---------------------------------------------------
    TimeSpan--表示一段时间,TimeSpan.Zero表示两段时间没有间隔
    DateTime.Now.AddMinutes(2)--表示在当前的时间上再加上2分钟(Minute)
      

  2.   

    在 Cache 中存储数据的最简单的方法就是使用一个键为其赋值,就像 HashTable 或 Dictionary 对象一样:Cache["key"] = "value";这种做法将在缓存中存储项,同时不带任何依赖项,因此它不会到期,除非缓存引擎为了给其他缓存数据提供空间而将其删除。要包括特定的缓存依赖项,可使用 Add() 或 Insert() 方法。其中每个方法都有几个重载。Add() 和 Insert() 之间的唯一区别是,Add() 返回对已缓存对象的引用,而 Insert() 没有返回值(在 C# 中为空,在 VB 中为 Sub)。