• ASP.NET缓存:缓存应用程序数据


    • 添加应用程序缓存项

    1、添加应用程序缓存项可以通过直接指定Cache对象的键值、Cache对象的Insert方法、Cache对象的Add方法实现。

    2、Cache对象的Insert方法有多个重载方法,通过重载可以指定创建缓存项的依赖,过期时间策略,优先级。

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

    4、Add方法没有重载方法;Add方法添加缓存项,将返回缓存中的对象;如果使用Add方法,缓存中已经存在与现有项同名的缓存项,则已存在的缓存项不会替换,并且不会引发异常。

                /**********通过键和值直接设置项向缓存添加项***********/
                Cache["keyCache1"] = "valueCache1";
    
    
                /**********通过使用 Insert 方法将项添加到缓存中***********/
                Cache.Insert("keyCache2", "valueCache2");
    
    
                /**********通过指定依赖项向缓存添加项***********/
                string[] dependencies = { "keyCache2" };
                Cache.Insert("keyCache3", "valueCache3",
                    new System.Web.Caching.CacheDependency(null, dependencies));
    
    
                /**********通过指定依赖项向缓存添加项(依赖指定应用缓存)***********/
                Cache.Insert("keyCache4", "valueCache4",
                    new System.Web.Caching.CacheDependency(
                        Server.MapPath("XMLFile.xml")));
    
    
                /**********通过指定依赖项向缓存添加项(依赖指定文件组)***********/
                string[] fileNames;
                string fileName1 = Server.MapPath("txtFile.txt");
                string fileName2 = Server.MapPath("xmlFile.xml");
                fileNames = new string[] { fileName1, fileName2 };
                Cache.Insert("keyCache5", "valueCache5",
                    new System.Web.Caching.CacheDependency(
                        fileNames));
    
    
                /**********通过指定依赖项向缓存添加项(依赖指定键和文件)***********/
                System.Web.Caching.CacheDependency dep1 =
                    new System.Web.Caching.CacheDependency(Server.MapPath("xmlFile.xml"));
                string[] keyDependcies2 = { "keyCache1" };
                System.Web.Caching.CacheDependency dep2 =
                    new System.Web.Caching.CacheDependency(null, keyDependcies2);
                System.Web.Caching.AggregateCacheDependency manyDep =
                    new System.Web.Caching.AggregateCacheDependency();
                manyDep.Add(dep1);
                manyDep.Add(dep2);
                Cache.Insert("keyCache6", "valueCache6", manyDep);
    
    
               /**********将设有过期策略的项添加到缓存中(绝对过期时间)***********/
                //设置了绝对过期,则不能设置滑动过期
                Cache.Insert("keyCache7", "valueCache7",
                    null, DateTime.Now.AddMinutes(1d),
                    System.Web.Caching.Cache.NoSlidingExpiration);
    
    
              /**********将设有过期策略的项添加到缓存中(滑动过期时间)***********/
                //如果设置了滑动过期,则不能设置绝对过期
                Cache.Insert("keyCache8", "valueCache8",
                    null, System.Web.Caching.Cache.NoAbsoluteExpiration,
                    new TimeSpan(0, 10, 0));
    
    
              /**********将设有优先级设置的项添加到缓存中***********/
                //AboveNormal 优先级高于Normal
                //BelowNormal 优先级低于Normal
              //Default 默认值为Normal优先级
                //High 在释放内存时,该优先级最不可能从缓存中移除
                //Low 该优先级最低
                //Normal 该优先级高于BelowNormal
              //NotRemovable 该优先级不会自动从缓存中移除,但受绝对时间和调整时间影响
                Cache.Insert("keyCache9", "valueCache9",
                    null, System.Web.Caching.Cache.NoAbsoluteExpiration,
                    System.Web.Caching.Cache.NoSlidingExpiration,
                    System.Web.Caching.CacheItemPriority.High, null);
    
    
                /**********使用 Add 方法向缓存添加项***********/
                //Add方法没有重载
                //Add方法将返回该缓存的值
                string valueCache10 = Cache.Add("keyCache10", "valueCache10",
                    null, System.Web.Caching.Cache.NoAbsoluteExpiration,
                    System.Web.Caching.Cache.NoSlidingExpiration,
    System.Web.Caching.CacheItemPriority.Default,
    null);
    •  检索应用程序缓存项的值
                string cachedString;
                cachedString = (string)Cache["keyCache1"];
                if (cachedString == null)
                {
                    cachedString = "valueCache1";
                    Cache.Insert("keyCache1", cachedString);
                }
    •  从缓存中移除缓存项

         Asp.net缓存中的数据易丢失。丢失原因:缓存已满,缓存项过期,依赖项发生改变。

            Cache.Remove("keyCache1");
  • 相关阅读:
    Nubiers to follow
    pp to write
    Reading source code
    build opencv with python support
    add monitor resolution
    Install cv2.so for Anaconda
    axios 上传下载显示进度
    vant 上传图片 图片回显 是base64
    多个时间段 合并
    热部署
  • 原文地址:https://www.cnblogs.com/wanghonghu/p/2531482.html
Copyright © 2020-2023  润新知