Using system;
Using system. Web;
Using system. collections;
Namespace eblog. Cache
{
/// <Summary>
/// Cache-related operations
/// </Summary>
Public class datacache
{
/// <Summary>
/// Obtain the cache value of the specified cachekey of the current application
/// </Summary>
/// <Param name = "cachekey"> </param>
/// <Returns> </returns>
Public static object getcache (string cachekey)
{
System. Web. caching. cache objcache = httpruntime. cache;
Return objcache [cachekey];
}
/// <Summary>
/// Whether the cache exists
/// </Summary>
/// <Param name = "cachekey"> </param>
/// <Returns> </returns>
Public static bool exists (string cachekey)
{
System. Web. caching. cache objcache = httpruntime. cache;
Return objcache [cachekey]! = NULL;
}
/// <Summary>
/// Set the cache value of the specified cachekey for the current application
/// </Summary>
/// <Param name = "cachekey"> </param>
/// <Param name = "objobject"> </param>
Public static void setcache (string cachekey, object objobject)
{
System. Web. caching. cache objcache = httpruntime. cache;
Objcache. insert (cachekey, objobject );
}
/// <Summary>
/// Set the cache value of the specified cachekey for the current application
/// </Summary>
/// <Param name = "cachekey"> </param>
/// <Param name = "objobject"> </param>
/// <Param name = "absoluteexpiration"> </param>
/// <Param name = "slidingexpiration"> </param>
Public static void setcache (string cachekey, object objobject, datetime absoluteexpiration, timespan slidingexpiration)
{
System. Web. caching. cache objcache = httpruntime. cache;
Objcache. insert (cachekey, objobject, null, absoluteexpiration, slidingexpiration );
}
Public static void setcache (string cachekey, object objobject, int minutes)
{
Setcache (cachekey, objobject, datetime. Now. addminutes (minutes), timespan. Zero );
}
Public static void removecache (string cachekey)
{
System. Web. caching. cache objcache = httpruntime. cache;
Objcache. Remove (cachekey );
}
# Region clear all caches
/// <Summary>
/// You may need to update the cache immediately. You must clear the cache manually.
/// The cache class has a remove method, but this method requires a cachekey, but we cannot know the cachekey of the entire website.
/// Only traversal is allowed
/// </Summary>
Public static void removeallcache ()
{
System. Web. caching. cache = httpruntime. cache;
Idictionaryenumerator cacheenum = cache. getenumerator ();
Arraylist Al = new arraylist ();
While (cacheenum. movenext ())
{
Al. Add (cacheenum. Key );
}
Foreach (string key in Al)
{
Cache. Remove (key );
}
}
# Endregion
# Region show all caches
// Display all caches
Public static string show ()
{
String STR = "";
Idictionaryenumerator cacheenum = httpruntime. cache. getenumerator ();
While (cacheenum. movenext ())
{
STR + = "<br/> cache name <B Title =" + cacheenum. value. tostring () + "> [" + cacheenum. key + "] </B> <br/> ";
}
Return "total current cache count:" + httpruntime. cache. Count + "<br/>" + STR;
}
# Endregion
}
}