網站首頁 編程語言 正文
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
相關推薦
- 2023-12-15 Linux系統中date命令、hwclock命令 語法詳解
- 2022-10-29 Pytorch訓練模型時如何釋放GPU顯存 torch.cuda.empty_cache()內存釋放
- 2022-07-11 Verilog中reg和SystemVerilog中logic的區別
- 2022-07-08 C#四種計時器Timer的區別和用法_C#教程
- 2022-09-15 docker倉庫登錄及配置insecure-registries的方法_docker
- 2022-05-14 shell腳本如何讀取properties文件中的值_linux shell
- 2022-04-08 Swift使用表格組件實現單列表_Swift
- 2022-03-17 解決.Net?Core項目發布在IIS上訪問404的問題_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支