佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > ASP.NET清空緩存時(shí)遇到的問題簡析

ASP.NET清空緩存時(shí)遇到的問題簡析

熱門標(biāo)簽:科大訊飛語音識別系統(tǒng) 地方門戶網(wǎng)站 集中運(yùn)營管理辦法 阿里云 網(wǎng)站排名優(yōu)化 硅谷的囚徒呼叫中心 百度競價(jià)排名 服務(wù)器配置

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

Liststring> keys = new Liststring>(); 
   // 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等數(shù)據(jù)源的緩存也是保存在HttpRuntime.Cache中,經(jīng)過測試沒想到竟然不是,因?yàn)閳?zhí)行上面的代碼以后ObjectDataSource仍然是從緩存讀取數(shù)據(jù)。

使用Reflector反編譯發(fā)現(xiàn)ObjectDataSource是使用HttpRuntime.CacheInternal來實(shí)現(xiàn)的緩存。CacheInternal是internal的,因此沒法直接寫代碼調(diào)用,同時(shí)CacheInternal中也沒提供清空緩存的方法,只能通過實(shí)驗(yàn)發(fā)現(xiàn)_caches._entries是保存緩存的Hashtable,因此就用反射的方法調(diào)用CacheInternal,然后拿到_caches._entries,最后clear才算ok。

最終代碼如下:

//HttpRuntime下的CacheInternal屬性(Internal的,內(nèi)存中是CacheMulti類型)是
ObjectDataSource等DataSource保存緩存的管理器 
//因?yàn)镃acheInternal、_caches、_entries等都是internal或者private的,
所以只能通過反射調(diào)用,而且可能會隨著.Net升級而失效 
 object cacheIntern = CommonHelper.GetPropertyValue(typeof(HttpRuntime), "CacheInternal") as IEnumerable; 
 //_caches是CacheMulti中保存多CacheSingle的一個(gè)IEnumerable字段。 
 IEnumerable _caches = CommonHelper.GetFieldValue(cacheIntern, "_caches") as IEnumerable; 
 foreach (object cacheSingle in _caches) 
 { 
  ClearCacheInternal(cacheSingle); 
 } 
 
private static void ClearCacheInternal(object cacheSingle) 
{ 
 //_entries是cacheSingle中保存緩存數(shù)據(jù)的一個(gè)private Hashtable 
 Hashtable _entries = CommonHelper.GetFieldValue(cacheSingle, "_entries") as Hashtable; 
 _entries.Clear(); 
} 
 
mary> 
/// 得到type類型的靜態(tài)屬性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的方法進(jìn)行調(diào)用,可能有潛在的問題,因此僅供參考。

在google上搜索到另外一篇文章,主干是代碼,代碼的思路和我一樣,貼過來也供參考。

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 ); 
 
 Listobject> keys = new Listobject>(); 
 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 } ); 
 } 
}

以上就是對ASP.NET清空緩存時(shí)遇到問題詳細(xì)分析,為了讓大家更好地解決此類問題,希望本文對大家的學(xué)習(xí)有所幫助。

您可能感興趣的文章:
  • ASP.net Substitution 頁面緩存而部分不緩存的實(shí)現(xiàn)方法
  • asp.net 客戶端瀏覽器緩存的Http頭介紹
  • asp.net 提高網(wǎng)站速度及如何利用緩存
  • asp.net(C#)遍歷memcached緩存對象
  • asp.net 使用駐留在頁面中的Cache緩存常用可定時(shí)更新的數(shù)據(jù)
  • 解決asp.net Sharepoint無法連接發(fā)布自定義字符串處理程序,不能進(jìn)行輸出緩存處理的方法
  • ASP.NET性能優(yōu)化之讓瀏覽器緩存動(dòng)態(tài)網(wǎng)頁的方法
  • ASP.NET緩存介紹
  • ASP.NET網(wǎng)站管理系統(tǒng)退出 清除瀏覽器緩存,Session的代碼
  • ASP.NET緩存管理的幾種方法
  • ASP.NET 4中的可擴(kuò)展輸出緩存(可以緩存頁面/控件等)
  • asp.net中Session緩存與Cache緩存的區(qū)別分析
  • ASP.NET頁面在IE緩存的清除辦法
  • ASP.NET 清除模式窗口數(shù)據(jù)緩存的操作方式
  • 設(shè)置ASP.NET頁面不被緩存(客戶端/服務(wù)器端取消緩存方法)
  • Asp.net禁用頁面緩存的方法總結(jié)

標(biāo)簽:烏蘭察布 隨州 西雙版納 甘孜 廣西 梧州 威海 開封

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET清空緩存時(shí)遇到的問題簡析》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    巴林右旗| 惠水县| 沾化县| 铜梁县| 吉木萨尔县| 清水县| 洛扎县| 蒙自县| 名山县| 梁山县| 三门县| 漠河县| 长白| 巨野县| 舒兰市| 阿拉善右旗| 称多县| 泸溪县| 靖远县| 古丈县| 安宁市| 桦南县| 海南省| 绥江县| 桓仁| 阳曲县| 张家界市| 曲周县| 白沙| 西盟| 大厂| 平安县| 彭阳县| 威海市| 石家庄市| 平塘县| 荣昌县| 辽源市| 乳山市| 甘孜县| 丹棱县|