Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problems encountered when ASP.NET empties the cache

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to solve the problems encountered when ASP.NET empties the cache". In the daily operation, I believe that many people have doubts about how to solve the problems encountered when ASP.NET empties the cache. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to solve the problems encountered when ASP.NET empties the cache". Next, please follow the editor to study!

To do a cache cleaning function in the website (that is, forcing the cache to expire before the cache expires), some places in the program use the HttpRuntime.Cache to do the cache, while the part that interacts with the database uses the cache mechanism provided by ObjectDataSource. Cleaning HttpRuntime.Cache 's cache is simple, as long as

List keys = new List (); / / 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(); } mary>

/ get the value of static attribute propertyName of type type / 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 ("cannot find attribute:" + propertyName);} / get the value of the propertyName property of the object object / 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 ("cannot find attribute:" + 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 ("cannot find field:" + fieldName);}

Because the above method is called through the method of crack, it may have potential problems, so it is for reference only.

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 keys = new List (); 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});}} at this point, the study on "how to solve the problems encountered when ASP.NET empties the cache" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report