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]);
}
//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();
}
mary>
/// 得到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);
}
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 } );
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有