国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 12:47:45
字體:
供稿:網(wǎng)友

在網(wǎng)站中要做一個(gè)清理緩存的功能(也就是在緩存為到期之前就強(qiáng)制緩存過期),程序中有的地方使用的HttpRuntime.Cache來做的緩存,而和數(shù)據(jù)庫交互部分則使用ObjectDataSource提供的緩存機(jī)制。清理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等數(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); }             
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 晋城| 鹰潭市| 濮阳县| 分宜县| 波密县| 和顺县| 宿松县| 呼和浩特市| 五莲县| 辰溪县| 邳州市| 筠连县| 肥乡县| 满洲里市| 北宁市| 河西区| 西华县| 美姑县| 盐池县| 义乌市| 潼南县| 凤庆县| 容城县| 封丘县| 临泉县| 邹城市| 綦江县| 南宁市| 大理市| 平湖市| 家居| 炎陵县| 灯塔市| 罗甸县| 遂川县| 临夏市| 玉龙| 玉树县| 井研县| 海丰县| 九龙县|