温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中缓存System.Web.Caching怎么用

发布时间:2022-04-26 13:37:56 来源:亿速云 阅读:129 作者:iii 栏目:开发技术

今天小编给大家分享一下C#中缓存System.Web.Caching怎么用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

System.Web.Caching.Cache Insert和Add方法的区别

Add()

object Add(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);

Insert()

void Insert(string key, object value); //永不过期
void Insert(string key, object value, CacheDependency dependencies);  //依赖
void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);//绝对时间过期:
void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback);  //依赖+回调
void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);  //依赖+优先级+回调

比较、区别

a). Insert方法支持5种重载,使用灵活,而Add方法必须提供7个参数;

b). Add方法可以返回缓存项的数据对象,Insert 返回Void;

c). 添加重复缓存情况下(Key已存在),Insert会替换该项,而Add方法则不执行任何操作,并返回原来保存的object对象(Update 2014-03-18)。

过期策略

  • a). 永不过期

Insert(string key, object value);

  • b). 绝对时间过期

DateTime.Now.AddSeconds(10)表示缓存在10秒后过期,TimeSpan.Zero表示不使用平滑过期策略。

例:Cache.Insert("Data", ds,null, DateTime.Now.AddSeconds(10), TimeSpan.Zero);

  • c). 变化时间过期(平滑过期)

DateTime.MaxValue表示不使用绝对时间过期策略,TimeSpan.FromSeconds(10)表示缓存连续10秒没有访问就过期。

例:Cache.Insert("Data", ds, null, DateTime.MaxValue, TimeSpan.FromSeconds(10));

使用Remove清空所有Cache

概述清空缓存主要通过Remove()方法,但是只能通过传入一个Key,清空一个。GetEnumerator()方法用于获取所有缓存项。MoveNext()用于将位置移动到下一个缓存项。如果想清空所有缓存,由于Cache类没有提供RemoveAll()方法,所以可以通过以下方式实现:

        public void removeAllCache()
        {
            IDictionaryEnumerator DicCache = HttpRuntime.Cache.GetEnumerator();
            int count = HttpRuntime.Cache.Count;
            for (int i = 0; i < count; i++)
            {
                DicCache.MoveNext();
                HttpRuntime.Cache.Remove(DicCache.Entry.Key.ToString());
            }
        }

存放缓存

 			#region 存放对应缓存
            Cache cache = HttpRuntime.Cache;
            //文件缓存依赖
            cache.Insert("CC", "依赖项测试", new CacheDependency(@"D:\123.txt"));
            //这时候在about.aspx页面添加一行代码,当更改一下D:123.txt时,cache["cc"]会立即被清空


            //30秒后就到期,立即移除,没商量
            cache.Insert("DD", "绝对过期测试", null, DateTime.Now.AddSeconds(30), System.Web.Caching.Cache.NoSlidingExpiration);


            //弹性过期时间,当缓存没使用10秒就过期
            cache.Insert("EE", "滑动过期测试", null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(10));

            //文件权重级别
            cache.Add("FF", "缓存重要级别", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30), CacheItemPriority.High, null);
            //在服务器释放系统内存时,具有该优先级级别的缓存项最有可能被从缓存删除。
            //Low = 1,-------------在服务器释放系统内存时,具有该优先级级别的缓存项比分配了 System.Web.Caching.CacheItemPriority.Normal优先级的项更有可能被从缓存删除。
            //BelowNormal = 2,---------------在服务器释放系统内存时,具有该优先级级别的缓存项很有可能被从缓存删除,其被删除的可能性仅次于具有 System.Web.Caching.CacheItemPriority.Low
            //Normal = 3,-------------------缓存项优先级的默认值为 System.Web.Caching.CacheItemPriority.Normal。
            //Default = 3,----------------在服务器释放系统内存时,具有该优先级级别的缓存项被删除的可能性比分配了 System.Web.Caching.CacheItemPriority.Normal优先级的项要小。
            //AboveNormal = 4,-------------在服务器释放系统内存时,具有该优先级级别的缓存项最不可能被从缓存删除。
            //High = 5,-------------------在服务器释放系统内存时,具有该优先级级别的缓存项将不会被自动从缓存删除。但是,具有该优先级级别的项会根据项的绝对到期时间或可调整到期时间与其他项一起被移除
            // NotRemovable = 6,


            //文件权重级别+Callback
            cache.Add("GG", "缓冲移除通知", null, DateTime.Now.AddSeconds(10), Cache.NoSlidingExpiration, CacheItemPriority.Low, Show);

            #endregion
        //回调
        public void Show(string key, object value, CacheItemRemovedReason reason)
        {
            Cache cache = HttpRuntime.Cache;
            Cache.Insert("GG", "缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!缓存被清空啦!");
        }

获取缓存

            #region 获取对应缓存
            //直接打开本页面,输出缓存依赖项测试
            //当更改D:\123.txt之后,在刷新,输出空,表明该Cache是依赖于D:\123.txt的
            Response.Write(HttpContext.Current.Cache["CC"]);


            //持续刷新30后,不会再输出  绝对过期测试
            Response.Write(HttpContext.Current.Cache["DD"]);


            //如果一直不停地刷新,都会继续输出,但是当超过10秒后再刷新,不会再输出   滑动缓存测试
            Response.Write(HttpContext.Current.Cache["EE"]);

            //文件权重级别
            Response.Write(HttpRuntime.Cache["FF"]);

            //测试回调函数
            Response.Write(HttpRuntime.Cache["GG"]);

            #endregion

以上就是“C#中缓存System.Web.Caching怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI