Insert 方法向缓存添加项,并且已经存在与现有项同名的项,则缓存中的现有项将被替换。

Add 方法,并且缓存中已经存在与现有项同名的项,则该方法不会替换该项,并且不会引发异常。

本主题中的过程阐释了向应用程序缓存添加项的如下方式:

  • 通过键和值直接设置项,向缓存添加项。

  • Insert 方法向缓存添加项。

  • 可以基于其他缓存项、文件和多个对象设置依赖项。

  • 您可以定义绝对过期时间或弹性过期时间,但不能同时定义两者。

  • 相对优先级帮助 .NET Framework 确定要移除的缓存项;较低优先级的项比较高优先级的项先从缓存中移除。

  • Add 方法添加项。

使用 SqlCacheDependency 类在 ASP.NET 中缓存

如何:从缓存中移除项时通知应用程序

通过键和值直接设置项向缓存添加项

  • 通过指定项的键和值,像将项添加到字典中一样将其添加到缓存中。

    Cache 对象中:

     
    Cache["CacheItem1"] = "Cached Item 1";
    

     

     

通过使用 Insert 方法将项添加到缓存中

  • Insert 方法,传递要添加的项的键和值。

    CacheItem2 的字符串:

     
    Cache.Insert("CacheItem2", "Cached Item 2");
    

     

     

通过指定依赖项向缓存添加项

  • CacheDependency 对象的一个实例传递给该方法

    CacheItem2 的另一个项:

     
    string[] dependencies = { "CacheItem2" };
    Cache.Insert("CacheItem3", "Cached Item 3",
        new System.Web.Caching.CacheDependency(null, dependencies));
    

     

     

    CacheItem4 的项添加到缓存中,并且在名为 XMLFile.xml 的文件上设置文件依赖项:

     
    Cache.Insert("CacheItem4", "Cached Item 4",
        new System.Web.Caching.CacheDependency(
        Server.MapPath("XMLFile.xml")));
    

     

     

    CacheItem1 的另一个项添加键依赖项,向名为 XMLFile.xml 的文件添加文件依赖项。

     
    System.Web.Caching.CacheDependency dep1 = 
        new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));
    string[] keyDependencies2 = { "CacheItem1" };
    System.Web.Caching.CacheDependency dep2 = 
        new System.Web.Caching.CacheDependency(null, keyDependencies2);
    System.Web.Caching.AggregateCacheDependency aggDep = 
        new System.Web.Caching.AggregateCacheDependency();
    aggDep.Add(dep1);
    aggDep.Add(dep2);
    Cache.Insert("CacheItem5", "Cached Item 5", aggDep);

将设有过期策略的项添加到缓存中

  • Insert 方法,将绝对过期时间或弹性过期时间传递给该方法。

    下面的代码示例将有一分钟绝对过期时间的项添加到缓存中:

     
    Cache.Insert("CacheItem6", "Cached Item 6",
        null, DateTime.Now.AddMinutes(1d), 
        System.Web.Caching.Cache.NoSlidingExpiration);
    

     

     

    下面的代码示例将有 10 分钟弹性过期时间的项添加到缓存中:

     
    Cache.Insert("CacheItem7", "Cached Item 7",
        null, System.Web.Caching.Cache.NoAbsoluteExpiration,
        new TimeSpan(0, 10, 0));
    

     

     

   将设有优先级设置的项添加到缓存中

  • CacheItemPriority 枚举中指定一个值。

    High 的项添加到缓存中:

     
    Cache.Insert("CacheItem8", "Cached Item 8",
        null, System.Web.Caching.Cache.NoAbsoluteExpiration,
        System.Web.Caching.Cache.NoSlidingExpiration,
        System.Web.Caching.CacheItemPriority.High, null);
    

     

     

使用 Add 方法向缓存添加项

  • Add 方法,它返回一个表示项的对象。

    CachedItem9 的值设置为已添加的项。

     
    string CachedItem9 = (string)Cache.Add("CacheItem9",
        "Cached Item 9", null,
        System.Web.Caching.Cache.NoAbsoluteExpiration,
        System.Web.Caching.Cache.NoSlidingExpiration, 
        System.Web.Caching.CacheItemPriority.Default,
        null);
    

     

     

相关文章:

  • 2022-01-29
  • 2022-12-23
  • 2022-01-18
  • 2021-05-19
  • 2021-10-06
  • 2021-12-03
  • 2021-12-19
  • 2021-04-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-13
  • 2021-05-31
  • 2022-01-15
相关资源
相似解决方案