Use Case of cache class in Asp.net (with source code)

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. collections;
Using system. Web;

Namespace Utility
{
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>
/// 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>
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 );
}
/// <Summary>
/// Clear the single-key Cache
/// </Summary>
/// <Param name = "key"> </param>
Public static void removeonecache (string cachekey)
{
System. Web. caching. cache objcache = httpruntime. cache;
Objcache. Remove (cachekey );
}
/// <Summary>
/// Clear all caches
/// </Summary>
Public static void removeallcache ()
{
System. Web. caching. cache _ cache = httpruntime. cache;
Idictionaryenumerator cacheenum = _ cache. getenumerator ();
If (_ cache. Count> 0)
{
Arraylist Al = new arraylist ();
While (cacheenum. movenext ())
{
Al. Add (cacheenum. Key );
}
Foreach (string key in Al)
{
_ Cache. Remove (key );
}
}
}
/// <Summary>
/// Return all existing caches in the form of a list
/// </Summary>
/// <Returns> </returns>
Public static arraylist showallcache ()
{
Arraylist Al = new arraylist ();
System. Web. caching. cache _ cache = httpruntime. cache;
If (_ cache. Count> 0)
{
Idictionaryenumerator cacheenum = _ cache. getenumerator ();
While (cacheenum. movenext ())
{
Al. Add (cacheenum. Key );
}
}
Return al;
}
}

}

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.