清除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做缓存。CacheInternalinternal的,因此没法直接写代码调用,同时CacheInternal中也没提供清空缓存的方法,只能通过实验发现_caches._entries是保存缓存的Hashtable,因此就用反射的方法调用CacheInternal,然后拿到_caches._entries,最后clear才算ok

  最终代码如下:

  //HttpRuntime下的CacheInternal属性(Internal的,内存中是CacheMulti类型)是ObjectDataSourceDataSource保存缓存的管理器

  //因为CacheInternal_caches_entries等都是internal或者private的,所以只能通过反射调用,而且可能会随着.Net升级而失效

  object cacheIntern = CommonHelper.GetPropertyValue(typeof(HttpRuntime), "CacheInternal") as IEnumerable;

  //_cachesCacheMulti中保存多CacheSingle的一个IEnumerable字段。

  IEnumerable _caches = CommonHelper.GetFieldValue(cacheIntern, "_caches") as IEnumerable;

  foreach (object cacheSingle in _caches)

  {

  ClearCacheInternal(cacheSingle);

  }

  private static void ClearCacheInternal(object cacheSingle)

  {

  //_entriescacheSingle中保存缓存数据的一个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的方法进行调用,可能有潜在的问题,因此仅供参考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值