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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

清除ASP.Net缓存

發布時間:2024/1/17 asp.net 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 清除ASP.Net缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在網站中要做一個清理緩存的功能(也就是在緩存為到期之前就強制緩存過期),程序中有的地方使用的HttpRuntime.Cache來做的緩存,而和數據庫交互部分則使用ObjectDataSource提供的緩存機制。清理HttpRuntime.Cache的緩存很簡單,只要

List<string> keys = new List<string>();
??????????? // retrieve application Cache enumerator
??????????? IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
??????????? // copy all keys that currently exist in Cache
??????????? while (enumerator.MoveNext())
??????????? {
??????????????? keys.Add(enumerator.Key.ToString());
??????????? }
??????????? // delete every key from cache
??????????? for (int i = 0; i < keys.Count; i++)
??????????? {
??????????????? HttpRuntime.Cache.Remove(keys[i]);
??????????? }

就可以了。

本以為ObjectDataSource等數據源的緩存也是保存在HttpRuntime.Cache中,經過測試沒想到竟然不是,因為執行上面的代碼以后ObjectDataSource仍然是從緩存讀取數據。

使用Reflector反編譯發現ObjectDataSource是使用HttpRuntime.CacheInternal來實現的緩存,氣氛呀,為什么微軟總愛搞“特殊化”,對外提供一個Cache用,自己偷偷用CacheInternal做緩存。CacheInternal是internal的,因此沒法直接寫代碼調用,同時CacheInternal中也沒提供清空緩存的方法,只能通過實驗發現_caches._entries是保存緩存的Hashtable,因此就用反射的方法調用CacheInternal,然后拿到_caches._entries,最后clear才算ok。

最終代碼如下:

??????????? //HttpRuntime下的CacheInternal屬性(Internal的,內存中是CacheMulti類型)是ObjectDataSource等DataSource保存緩存的管理器
??????????? //因為CacheInternal、_caches、_entries等都是internal或者private的,所以只能通過反射調用,而且可能會隨著.Net升級而失效
??????????? object cacheIntern = CommonHelper.GetPropertyValue(typeof(HttpRuntime), "CacheInternal") as IEnumerable;
??????????? //_caches是CacheMulti中保存多CacheSingle的一個IEnumerable字段。
??????????? IEnumerable _caches = CommonHelper.GetFieldValue(cacheIntern, "_caches") as IEnumerable;
??????????? foreach (object cacheSingle in _caches)
??????????? {
??????????????? ClearCacheInternal(cacheSingle);
??????????? }

??????? private static void ClearCacheInternal(object cacheSingle)
??????? {
??????????? //_entries是cacheSingle中保存緩存數據的一個private Hashtable
??????????? Hashtable _entries = CommonHelper.GetFieldValue(cacheSingle, "_entries") as Hashtable;
??????????? _entries.Clear();
??????? }

/// <summary>
??????? /// 得到type類型的靜態屬性propertyName的值
??????? /// </summary>
??????? /// <param name="type"></param>
??????? /// <param name="propertyName"></param>
??????? /// <returns></returns>
??????? public static object GetPropertyValue(Type type, string propertyName)
??????? {
??????????? foreach (PropertyInfo rInfo in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance))
??????????? {
??????????????? if (rInfo.Name == propertyName)
??????????????? {
??????????????????? return rInfo.GetValue(null, new object[0]);
??????????????? }
??????????? }
??????????? throw new Exception("無法找到屬性:" + propertyName);
??????? }

??????? /// <summary>
??????? /// 得到object對象的propertyName屬性的值
??????? /// </summary>
??????? /// <param name="obj"></param>
??????? /// <param name="propertyName"></param>
??????? /// <returns></returns>
??????? public static object GetPropertyValue(object obj, string propertyName)
??????? {
??????????? Type type = obj.GetType();
??????????? foreach (PropertyInfo rInfo in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance))
??????????? {
??????????????? if (rInfo.Name == propertyName)
??????????????? {
??????????????????? return rInfo.GetValue(obj, new object[0]);
??????????????? }
??????????? }
??????????? throw new Exception("無法找到屬性:" + propertyName);
??????? }

??????? public static object GetFieldValue(object obj, string fieldName)
??????? {
??????????? Type type = obj.GetType();
??????????? foreach (FieldInfo rInfo in type.GetFields(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance))
??????????? {
??????????????? if (rInfo.Name == fieldName)
??????????????? {
??????????????????? return rInfo.GetValue(obj);
??????????????? }
??????????? }
??????????? throw new Exception("無法找到字段:" + fieldName);
??????? }

上面方法由于是通過crack的方法進行調用,可能有潛在的問題,因此僅供參考。

在google上搜索到另外一篇文章 http://www.msdnkk.hu/Articles/Clear_OutputCache-Minden_oldal_torlese?,由于是匈牙利文的,也看不懂在說什么,不過主干是代碼,看他代碼的思路和我一樣,貼過來也供參考

private void clearOutputCache()
{
??? Type ct = this.Cache.GetType();
??? FieldInfo cif = ct.GetField( "_cacheInternal", BindingFlags.NonPublic | BindingFlags.Instance );
??? Type cmt = Cache.GetType().Assembly.GetType( "System.Web.Caching.CacheMultiple" );
??? Type cachekeyType = Cache.GetType().Assembly.GetType( "System.Web.Caching.CacheKey" );
??? FieldInfo cachesfield = cmt.GetField( "_caches", BindingFlags.NonPublic | BindingFlags.Instance );

??? object cacheInternal = cif.GetValue( this.Cache );
??? object caches = cachesfield.GetValue( cacheInternal );

??? Type arrayType = typeof( Array );
??? MethodInfo arrayGetter = arrayType.GetMethod( "GetValue", new Type[] { typeof( int ) } );
??? object cacheSingle = arrayGetter.Invoke( caches, new object[] { 1 } );

??? FieldInfo entriesField = cacheSingle.GetType().GetField( "_entries", BindingFlags.Instance | BindingFlags.NonPublic );
??? Hashtable entries = (Hashtable) entriesField.GetValue( cacheSingle );

??? List<object> keys = new List<object>();
??? foreach( object o in entries.Keys )
??? {
??????? keys.Add( o );
??? }

??? MethodInfo remove = cacheInternal.GetType().GetMethod( "Remove", BindingFlags.NonPublic | BindingFlags.Instance, null,
??????? new Type[] { cachekeyType, typeof( CacheItemRemovedReason ) }, null );
??? foreach( object key in keys )
??? {
??????? remove.Invoke( cacheInternal, new object[] { key, CacheItemRemovedReason.Removed } );
??? }
}

如鵬網.Net培訓班正在報名,有網絡的地方就可以參加如鵬網的學習,學完就能高薪就業,點擊此處了解

?

? ??三年前只要懂“三層架構”就可以說“精通分層架構”;現在則需要懂IOC(AutoFac等)、CodeFirst、lambda、DTO等才值錢;

????三年前只要會SQLServer就可以說自己“精通數據庫開發”;現在則需還需要掌握MySQL等開源數據庫才能說是“.Net開源”時代的程序員;

????三年前只要會進行用戶上傳內容的安全性處理即可;現在則需要熟悉云存儲、CDN等才能在云計算時代游刃有余;

????三年前只要掌握Lucene.Net就會說自己“熟悉站內搜索引擎開發”;現在大家都用ElasticSearch了,你還用Lucene.Net就太老土了;

????三年前發郵件還是用SmtpClient;現在做大型網站發郵件必須用云郵件引擎;

????三年前緩存就是Context.Cache;現在則是Redis、Memcached的天下;

????如鵬網再次引領.Net社區技術潮流!點擊此處了解如鵬網.Net最新課程

總結

以上是生活随笔為你收集整理的清除ASP.Net缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。