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
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]);
}
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 cacheinternal attribute in httpruntime (the memory type is cachemulti) is the manager for storing the cache by datasource such as objectdatasource.
// Because cacheinternal, _ caches, and _ entries are both internal or private, they can only be called through reflection and may expire with the. 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.
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 ("attributes cannot be found:" + propertyname );
}
/// Obtain the value of the propertyname attribute of the 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 ("attributes 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 ("the field cannot be found:" + fieldname );
}
The above method is called through the crack method, which may cause potential problems, so it is only for reference.
Look at other people's code. The idea is the same as mine. Post it for reference.
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 );
Listkeys = new list
Http://www.sewingmachinebag.com