Make a cache cleanup function on the website (that is, force cache expiration before the cache expires), and use httpruntime 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
The following is a code snippet: 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]); } |
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:
The following is a code snippet: // The cacheinternal attribute in httpruntime (the memory type is cachemulti) is The manager for storing data cached by datasource, such as objectdatasource // Because cacheinternal, _ caches, and _ entries are both internal or private, Therefore, it can only be called through reflection, and may expire with. net upgrade. Object cacheintern = commonhelper. getpropertyvalue (typeof (httpruntime), "cacheinternal") as ienumerable; // _ Caches is an ienumerable field that stores multiple cachesingle in cachemulti. Ienumerable _ caches = commonhelper. getfieldvalue (cacheintern, "_ caches") as ienumerable; Foreach (Object cachesingle in _ caches) { Abstracheinternal (cachesingle ); }Private Static void maid (Object cachesingle) { // _ Entries is a private hashtable that stores cached data in cachesingle. Hashtable _ entries = commonhelper. getfieldvalue (cachesingle, "_ entries") as hashtable; _ Entries. Clear (); } Mary> /// Obtain the value of the Static Property propertyname of the type. /// </Summary> /// <Param name = "type"> </param> /// <Param name = "propertyname"> </param> /// <Returns> </returns> Public static object getpropertyvalue (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 ("properties cannot be found:" + propertyname ); } /// <Summary> /// Obtain the value of the propertyname attribute of the object. /// </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 ("properties cannot be found:" + 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 ("field not found:" + fieldname ); } |
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
The following is a code snippet: 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 }); } } |
|