C# System.Runtime.Caching使用


System.Runtime.Caching命名空間是.NET 4.0新增的,目的是將以前的.NET 版本中的System.Web.Caching單獨提取出來,獨立使用,這樣web和其他.NET程序如WPF都可以使用。

System.Runtime.Caching包含緩存類和監視類,包括文件、數據庫、緩存等的監視,與以前在System.Web.Caching中的一樣,但重新包裝。

可以預見在以后的版本中,System.Web.Caching命名空間會被標記為Obsolete(過時),或者重寫為包裝System.Runtime.Caching中的方法。

 

using System.Runtime.Caching;
public static string GetToken()
        {
            ObjectCache oCache = MemoryCache.Default;
            string fileContents = oCache["wechart_token"] as string;
            if (fileContents == null)
            {
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Now.AddMinutes(120);//取得或設定值,這個值會指定是否應該在指定期間過后清除
                fileContents = //這里賦值;
                oCache.Set("wechart_token", fileContents, policy);
            }
            return fileContents;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM