C#缓存System.Web.Caching

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#缓存System.Web.Caching

農碼一生   2022-06-03 我要评论

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#中缓存Sytem.Web.Caching的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们