Analysis of problems encountered when ASP. NET clears the cache

Source: Internet
Author: User

A cache cleanup function is required on the website, that is, to force cache expiration before the cache expires. HttpRuntime is used in some places in the program. cache, while the interaction with the database uses the Cache mechanism provided by ObjectDataSource. It's easy to clear the HttpRuntime. Cache, as long

 
 
  1. List<string> keys = new List<string>();  
  2.             // retrieve application Cache enumerator  
  3. IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();  
  4.             // copy all keys that currently exist in Cache  
  5.             while (enumerator.MoveNext())  
  6.             {  
  7.                 keys.Add(enumerator.Key.ToString());  
  8.             }  
  9.             // delete every key from cache  
  10.             for (int i = 0; i < keys.Count; i++)  
  11.             {  
  12.                 HttpRuntime.Cache.Remove(keys[i]);  
  13.             } 

You can.

I thought that the Cache of data sources such as ObjectDataSource was also saved in HttpRuntime. Cache. I did not expect it after testing, because after executing the above Code, ObjectDataSource still reads data from the Cache.

Through Reflector decompilation, we found that ObjectDataSource is a Cache implemented using HttpRuntime. CacheInternal. In the atmosphere, why does Microsoft always like to "specialize" and provide a Cache for external use, and secretly uses CacheInternal for caching. CacheInternal is internal, so you cannot directly write code to call it. At the same time, CacheInternal does not provide a method to clear the cache. You can only find _ caches through experiments. _ entries is the Hashtable that saves the cache. Therefore, CacheInternal is called using the reflection method, and _ caches is obtained. _ entries, the last clear is OK.

The final code is as follows:

 
 
  1. // The CacheInternal attribute of HttpRuntime is Internal, and the memory type is CacheMulti.) Yes
  2. The manager for storing data cached by DataSource, such as ObjectDataSource
  3. // Because CacheInternal, _ caches, and _ entries are both internal or private,
  4. Therefore, it can only be called through reflection, and may expire with. Net upgrade.
  5. Object cacheIntern = CommonHelper. GetPropertyValue (typeof (HttpRuntime), "CacheInternal") as IEnumerable;
  6. // _ Caches is an IEnumerable field that stores multiple CacheSingle in CacheMulti.
  7. IEnumerable _ caches = CommonHelper. GetFieldValue (cacheIntern, "_ caches") as IEnumerable;
  8. Foreach (object cacheSingle in _ caches)
  9. {
  10. Abstracheinternal (cacheSingle );
  11. }
  12.  
  13. Private static void maid (object cacheSingle)
  14. {
  15. // _ Entries is a private Hashtable that stores cached data in cacheSingle.
  16. Hashtable _ entries = CommonHelper. GetFieldValue (cacheSingle, "_ entries") as Hashtable;
  17. _ Entries. Clear ();
  18. }
  19.  
  20. Mary>
  21. /// Obtain the value of the Static Property propertyName of the type.
  22. /// </Summary>
  23. /// <Param name = "type"> </param>
  24. /// <Param name = "propertyName"> </param>
  25. /// <Returns> </returns>
  26. Public static object GetPropertyValue (Type type, string propertyName)
  27. {
  28. Foreach (PropertyInfo rInfo in type. GetProperties
  29. (BindingFlags. NonPublic | BindingFlags. Static | BindingFlags. Public | BindingFlags. Instance ))
  30. {
  31. If (rInfo. Name = propertyName)
  32. {
  33. Return rInfo. GetValue (null, new object [0]);
  34. }
  35. }
  36. Throw new Exception ("properties cannot be found:" + propertyName );
  37. }
  38.  
  39. /// <Summary>
  40. /// Obtain the value of the propertyName attribute of the object.
  41. /// </Summary>
  42. /// <Param name = "obj"> </param>
  43. /// <Param name = "propertyName"> </param>
  44. /// <Returns> </returns>
  45. Public static object GetPropertyValue (object obj, string propertyName)
  46. {
  47. Type type = obj. GetType ();
  48. Foreach (PropertyInfo rInfo in type. GetProperties
  49. (BindingFlags. NonPublic | BindingFlags. Static | BindingFlags. Public | BindingFlags. Instance ))
  50. {
  51. If (rInfo. Name = propertyName)
  52. {
  53. Return rInfo. GetValue (obj, new object [0]);
  54. }
  55. }
  56. Throw new Exception ("properties cannot be found:" + propertyName );
  57. }
  58.  
  59. Public static object GetFieldValue (object obj, string fieldName)
  60. {
  61. Type type = obj. GetType ();
  62. Foreach (FieldInfo rInfo in type. GetFields
  63. (BindingFlags. NonPublic | BindingFlags. Static | BindingFlags. Public | BindingFlags. Instance ))
  64. {
  65. If (rInfo. Name = fieldName)
  66. {
  67. Return rInfo. GetValue (obj );
  68. }
  69. }
  70. Throw new Exception ("field not found:" + fieldName );
  71. }

The above method is called through the crack method, which may cause potential problems, so it is only for reference.

Search for another article on google http://www.msdnkk.hu/Articles/Clear_OutputCache-Minden_oldal_torlese, because it is Hungary, also do not understand what to say, but the backbone is the code, look at his code ideas and I like, post it for reference

 
 
  1. private void clearOutputCache()  
  2. {  
  3.     Type ct = this.Cache.GetType();  
  4.     FieldInfo cif = ct.GetField( "_cacheInternal", BindingFlags.NonPublic | BindingFlags.Instance );  
  5.     Type cmt = Cache.GetType().Assembly.GetType( "System.Web.Caching.CacheMultiple" );  
  6.     Type cachekeyType = Cache.GetType().Assembly.GetType( "System.Web.Caching.CacheKey" );  
  7.     FieldInfo cachesfield = cmt.GetField( "_caches", BindingFlags.NonPublic | BindingFlags.Instance );  
  8.  
  9.     object cacheInternal = cif.GetValue( this.Cache );  
  10.     object caches = cachesfield.GetValue( cacheInternal );  
  11.  
  12.     Type arrayType = typeof( Array );  
  13.     MethodInfo arrayGetter = arrayType.GetMethod( "GetValue", new Type[] { typeof( int ) } );  
  14.     object cacheSingle = arrayGetter.Invoke( caches, new object[] { 1 } );  
  15.  
  16.     FieldInfo entriesField = cacheSingle.GetType().GetField( "_entries", BindingFlags.Instance | BindingFlags.NonPublic );  
  17.     Hashtable entries = (Hashtable) entriesField.GetValue( cacheSingle );  
  18.  
  19.     List<object> keys = new List<object>();  
  20.     foreach( object o in entries.Keys )  
  21.     {  
  22.         keys.Add( o );  
  23.     }  
  24.  
  25.     MethodInfo remove = cacheInternal.GetType().GetMethod( "Remove", BindingFlags.NonPublic | BindingFlags.Instance, null,  
  26.         new Type[] { cachekeyType, typeof( CacheItemRemovedReason ) }, null );  
  27.     foreach( object key in keys )  
  28.     {  
  29.         remove.Invoke( cacheInternal, new object[] { key, CacheItemRemovedReason.Removed } );  
  30.     }  

Original article title: Clear ASP. Net Cache

Link: http://www.cnblogs.com/rupeng/archive/2010/08/05/1793499.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.