C#中缓存System.Web.Caching用法总结

作者:農碼一生 时间:2021-09-05 04:41:11 

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

来源:https://www.cnblogs.com/wml-it/p/15813518.html

标签:C#,Sytem,Web,Caching,缓存
0
投稿

猜你喜欢

  • Spring Cache+Redis缓存数据的实现示例

    2023-11-26 11:53:20
  • Springcloud Config配置中心使用与相关介绍

    2021-07-13 05:15:17
  • Mybatis-Plus 全局配置无效的解决方案

    2022-06-29 12:46:02
  • @RequestBody的使用案例代码

    2021-07-11 16:46:50
  • Java转JSON串的几种方式

    2023-08-24 07:38:47
  • java web返回中文乱码问题及解决

    2023-08-25 08:23:08
  • 为什么阿里巴巴要求日期格式化时必须有使用y表示年

    2023-11-02 20:08:58
  • Java数据结构之二叉排序树的实现

    2023-07-05 02:27:25
  • JAVA基础之注解与反射的使用方法和场景

    2022-02-23 19:50:00
  • Java多线程之Semaphore实现信号灯

    2023-09-19 23:04:51
  • Spring Security基于JWT实现SSO单点登录详解

    2022-02-28 10:56:55
  • 三道java新手入门面试题,通往自由的道路--锁+Volatile

    2023-09-04 20:33:42
  • Spring创建bean的几种方式及使用场景

    2021-06-20 14:16:35
  • Intellij Idea中批量导入第三方jar包的全过程

    2022-01-29 03:14:26
  • java使用jdbc操作数据库示例分享

    2023-10-29 13:54:21
  • SpringBoot接口如何统一异常处理

    2023-08-10 15:06:20
  • JavaWeb如何实现禁用浏览器缓存

    2021-09-13 01:27:45
  • Java循环对bean的属性进行赋值的实现

    2023-01-27 10:18:05
  • Spring实战之方法级别缓存用法示例

    2022-10-20 02:00:32
  • IDEA下载并大学生edu邮箱认证免费使用教程(图文)

    2022-10-10 14:23:02
  • asp之家 软件编程 m.aspxhome.com