在网站中要做一个清理缓存的功能(也就是在缓存为到期之前就强制缓存过期),程序中有的地方使用的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来实现的缓存。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();
}
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);
}
上面方法由于是通过crack的方法进行调用,可能有潜在的问题,因此仅供参考。
在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 );
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 } );
}
}
以上就是对ASP.NET清空缓存时遇到问题详细分析,为了让大家更好地解决此类问题,希望本文对大家的学习有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。