一起谈.NET技术,浅析ASP.NET清空缓存时遇到的问题

简介:   相关文章:ASP.NET 缓存全解析  在网站中要做一个清理缓存的功能(也就是在缓存为到期之前就强制缓存过期),程序中有的地方使用的HttpRuntime.Cache来做的缓存,而和数据库交互部分则使用ObjectDataSource提供的缓存机制。

  相关文章: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做缓存。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();
}

/// <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的方法进行调用,可能有潜在的问题,因此仅供参考。

  在google上搜索到另外一篇文章 http://www.msdnkk.hu/Articles/Clear_OutputCache-Minden_oldal_torlese ,由于是匈牙利文的,也看不懂在说什么,不过主干是代码,看他代码的思路和我一样,贴过来也供参考:

 
 
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 } );
}
}
目录
相关文章
|
4天前
|
存储 消息中间件 缓存
Redis缓存技术详解
【5月更文挑战第6天】Redis是一款高性能内存数据结构存储系统,常用于缓存、消息队列、分布式锁等场景。其特点包括速度快(全内存存储)、丰富数据类型、持久化、发布/订阅、主从复制和分布式锁。优化策略包括选择合适数据类型、设置过期时间、使用Pipeline、开启持久化、监控调优及使用集群。通过这些手段,Redis能为系统提供高效稳定的服务。
|
16天前
|
存储 数据采集 缓存
软件体系结构 - 缓存技术(10)布隆过滤器
【4月更文挑战第20天】软件体系结构 - 缓存技术(10)布隆过滤器
22 0
|
16天前
|
缓存 数据库 UED
软件体系结构 - 缓存技术(9)缓存穿透
【4月更文挑战第20天】软件体系结构 - 缓存技术(9)缓存穿透
75 13
|
16天前
|
缓存 监控 前端开发
软件体系结构 - 缓存技术(8)缓存雪崩
【4月更文挑战第20天】软件体系结构 - 缓存技术(8)缓存雪崩
77 17
|
16天前
|
缓存 NoSQL Redis
软件体系结构 - 缓存技术(7)Redis持久化方法
【4月更文挑战第20天】软件体系结构 - 缓存技术(7)Redis持久化方法
87 14
|
16天前
|
缓存 监控 算法
软件体系结构 - 缓存技术(6)淘汰策略
【4月更文挑战第20天】软件体系结构 - 缓存技术(6)淘汰策略
87 12
|
16天前
|
存储 缓存 运维
软件体系结构 - 缓存技术(5)Redis Cluster
【4月更文挑战第20天】软件体系结构 - 缓存技术(5)Redis Cluster
140 10
|
16天前
|
存储 缓存 NoSQL
软件体系结构 - 缓存技术(4)Redis分布式存储
【4月更文挑战第20天】软件体系结构 - 缓存技术(4)Redis分布式存储
41 12
|
16天前
|
缓存 安全 网络安全
软件体系结构 - 缓存技术(3)Squid
【4月更文挑战第20天】软件体系结构 - 缓存技术(3)Squid
39 14
|
16天前
|
存储 缓存 NoSQL
软件体系结构 - 缓存技术(2)Redis
【4月更文挑战第20天】软件体系结构 - 缓存技术(2)Redis
53 12