日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

發(fā)布時間:2023/11/29 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

先看MSDN上的解釋:
????? HttpContext.Current.Cache:為當(dāng)前 HTTP 請求獲取Cache對象。
????? HttpRuntime.Cache:獲取當(dāng)前應(yīng)用程序的Cache。?
????? 我們再用.NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的實現(xiàn):

HttpContext.Cache和HttpRuntime.Cache實現(xiàn)
??? //System.Web.HttpContext.Cache屬性實現(xiàn)
??? public sealed class HttpContext
??? {
??????? public Cache Cache
??????? {
??????????? get
??????????? {
??????????????? return HttpRuntime.Cache;
??????????? }
??????? }
??? }


??? //System.Web.HttpRuntime.Cache屬性實現(xiàn)
??? public sealed class HttpRuntime
??? {
??????? public static Cache Cache
??????? {
??????????? get
??????????? {
??????????????? if (AspInstallDirectoryInternal == null)
??????????????? {
??????????????????? throw new HttpException(SR.GetString("Aspnet_not_installed", new object[] { VersionInfo.SystemWebVersion }));
??????????????? }
??????????????? Cache cache = _theRuntime._cachePublic;
??????????????? if (cache == null)
??????????????? {
??????????????????? CacheInternal cacheInternal = CacheInternal;
??????????????????? CacheSection cacheSection = RuntimeConfig.GetAppConfig().Cache;
??????????????????? cacheInternal.ReadCacheInternalConfig(cacheSection);
??????????????????? _theRuntime._cachePublic = cacheInternal.CachePublic;
??????????????????? cache = _theRuntime._cachePublic;
??????????????? }
??????????????? return cache;
??????????? }
??????? }
??? }

?????? 通過上面的代碼我們可以看出:HttpContext.Current.Cache是調(diào)用HttpRuntime.Cache實現(xiàn)的,兩者指向同一Cache對象。那么兩者到底有沒有區(qū)別的?既然兩個指向的是同一Cache對象,兩者的差別只能出現(xiàn)在HttpContext和HttpRuntime上了。我們再來看看MSDN中HttpContext和HttpRuntime的定義。
????? HttpContext:封裝有關(guān)個別HTTP請求的所有HTTP特定的信息,HttpContext.Current為當(dāng)前的HTTP請求獲取HttpContext對象。
????? HttpRuntime:為當(dāng)前應(yīng)用程序提供一組ASP.NET運行時服務(wù)。

????? 由上面的定義可以看出:HttpRuntime.Cache相當(dāng)于就是一個緩存具體實現(xiàn)類,這個類雖然被放在了System.Web命名空間下,但是非Web應(yīng)用下也是可以使用;HttpContext.Current.Cache是對上述緩存類的封裝,由于封裝到了HttpContext類中,局限于只能在知道HttpContext下使用,即只能用于Web應(yīng)用。

????? 下面的例子可以很好的說明這一點:

HttpContext.Cache和HttpRuntime.Cache的示例
??? class CacheTest
??? {
??????? static void Main(string[] args)
??????? {???????
??????????? System.Web.Caching.Cache httpRuntimeCache = System.Web.HttpRuntime.Cache;
??????????? httpRuntimeCache.Insert("httpRuntimeCache", "I am stored in HttpRuntime.Cache");

??????????? if (httpRuntimeCache != null)
??????????? {
??????????????? Console.WriteLine("httpRuntimeCache:" + httpRuntimeCache["httpRuntimeCache"]);
??????????? }

??????????? System.Web.HttpContext httpContext = System.Web.HttpContext.Current;
??????????? if (httpContext == null)
??????????? {
??????????????? Console.WriteLine("HttpContext object is null in Console Project");
??????????? }
??????????? else
??????????? {
??????????????? System.Web.Caching.Cache httpContextCache = httpContext.Cache;
??????????????? httpContextCache.Insert("httpContextCache", "I am stored in HttpRuntime.Cache");
??????????????? if (httpContextCache == null)
??????????????? {
??????????????????? Console.WriteLine("httpContextCache is null");
??????????????? }
??????????? }
?????????????
??????????? Console.ReadLine();
??????? }
??? }

????? 輸出結(jié)果:httpRuntimeCache:I am stored in HttpRuntime.Cache
????? HttpContext object is null in Console Project

????? 綜上:我們在使用Cache時,盡量使用HttpRuntime.Cache,既能減少出錯,也減少了一次函數(shù)調(diào)用。

????? 參考資料:HttpRuntime.Cache 與HttpContext.Current.Cache的疑問,HttpRuntime.Cache vs. HttpContext.Current.Cache

?

出處:http://blog.csdn.net/qwlovedzm/article/details/7024405

===============================================================================

下面我們看看簡單的緩存類處理:

using System; using System.Collections; using System.Web; using System.Web.Caching;namespace aaaaa.Api.Business {/// <summary>/// 緩存類/// </summary>public class CacheHelper{/// <summary>/// 增加一個緩存對象/// </summary>/// <param name="strKey">鍵值名稱</param>/// <param name="valueObj">被緩存對象</param>/// <param name="durationMin">緩存失效時間(默認(rèn)為5分鐘)</param>/// <param name="cachePriority">保留優(yōu)先級(枚舉數(shù)值)</param>/// <returns>緩存寫入是否成功true 、false</returns>public static bool InsertCach(string strKey, object valueObj, int durationMin,CacheItemPriority cachePriority = CacheItemPriority.Default){TimeSpan ts;if (!string.IsNullOrWhiteSpace(strKey) && valueObj != null){//onRemove是委托執(zhí)行的函數(shù),具體方法看下面的onRemove(...)CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);ts = durationMin == 0 ? new TimeSpan(0, 5, 0) : new TimeSpan(0, durationMin, 0);//HttpContext.Current.Cache.Insert( HttpRuntime.Cache.Insert(strKey,valueObj,null,DateTime.Now.Add(ts),Cache.NoSlidingExpiration,cachePriority,callBack);return true;}else{return false;}}/// <summary>/// 判斷緩存對象是否存在/// </summary>/// <param name="strKey">緩存鍵值名稱</param>/// <returns>是否存在true 、false</returns>public static bool IsExist(string strKey){//return HttpContext.Current.Cache[strKey] != null;return HttpRuntime.Cache.Get(strKey) != null;}/// <summary>/// 讀取緩存對象/// </summary>/// <param name="strKey">緩存鍵值名稱</param>/// <returns>緩存對象,objec類型</returns>public static object GetCache(string strKey){//if (HttpContext.Current.Cache[strKey] != null)if (IsExist(strKey)){object obj = HttpRuntime.Cache.Get(strKey);return obj ?? null;}else{return null;}}/// <summary>/// 移除緩存對象/// </summary>/// <param name="strKey">緩存鍵值名稱</param>public static void Remove(string strKey){//if (HttpContext.Current.Cache[strKey] != null)if (IsExist(strKey)){HttpRuntime.Cache.Remove(strKey);}}/// <summary>/// 清除所有緩存/// </summary>public static void Clear(){IDictionaryEnumerator enu = HttpRuntime.Cache.GetEnumerator();while (enu.MoveNext()){Remove(enu.Key.ToString());}}public static CacheItemRemovedReason reason;/// <summary>/// 此方法在值失效之前調(diào)用,可以用于在失效之前更新數(shù)據(jù)庫,或從數(shù)據(jù)庫重新獲取數(shù)據(jù)/// </summary>/// <param name="strKey"></param>/// <param name="obj"></param>/// <param name="reason"></param>private static void onRemove(string strKey, object obj, CacheItemRemovedReason r){reason = r;}//... } }

?

出處:http://blog.csdn.net/joyhen/article/details/40379145

=======================================================================

引用:System.Runtime.Caching.dll,如下測試,fm4.5

static void CacheTest(){string cname = "filescontents";ObjectCache cc = MemoryCache.Default;string fileContents = cc[cname] as string;if (fileContents == null){CacheItemPolicy policy = new CacheItemPolicy();TimeSpan sp = new TimeSpan(0, 1, 0);policy.SlidingExpiration = sp;List<string> filePaths = new List<string>();string path = System.IO.Directory.GetCurrentDirectory() + "\\example.txt";filePaths.Add(path);policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));fileContents = System.IO.File.ReadAllText(path, Encoding.Default);cc.Set(cname, fileContents, policy);}Console.WriteLine(fileContents);}static void Main(string[] args){//ExecuteCode(WriteData);//ExecuteCode(ReadData);//ExecuteCode(TransData);bool quit = false;while (!quit){Console.Write("get cache: ");string demo = Console.ReadLine();switch (demo){case "Y": ExecuteCode(CacheTest); break;case "Q":quit = true;break;default:Console.WriteLine("Choose a Word of Y and Q(to quit)");break;}}Console.ReadKey();}public static void ExecuteCode(Action a){System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();stopwatch.Start();a();stopwatch.Stop();TimeSpan timespan = stopwatch.Elapsed;Console.WriteLine("運行{0}秒", timespan.TotalSeconds);}

?

出處:http://blog.csdn.net/joyhen/article/details/39990455

轉(zhuǎn)載于:https://www.cnblogs.com/mq0036/p/7016677.html

總結(jié)

以上是生活随笔為你收集整理的HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。