Common tool classes 8-cache classes

Source: Internet
Author: User

public static Class Cachehelper
{
static Cache _cache = Httpruntime.cache;

<summary>
Get Cache
</summary>
<param name= "Key" ></param>
<returns></returns>
public static object Get (String key)
{
Return _cache. Get (key);
}

<summary>
Setting up the cache
</summary>
<param name= "Key" ></param>
<param name= "Value" ></param>
public static void Set (string key, Object value)
{
_cache. Insert (key, value);
}

<summary>
To set a valid cache for a specified time period
</summary>
<param name= "Key" ></param>
<param name= "Value" ></param>
<param name= "Expireseconds" > Cache time, units per second </param>
public static void Set (string key, object value, long Expireseconds)
{
_cache. Insert (key, value, NULL, DateTime.UtcNow.AddSeconds (expireseconds), TimeSpan.Zero);
}

<summary>
Clears the cached contents of the specified key
</summary>
<param name= "Key" ></param>
public static void Remove (String key)
{
_cache. Remove (key);
}

<summary>
Clears all cached content for a specified prefix for a keyword
</summary>
<param name= "Keyprefix" > Cache keyword prefix </param>
public static void Removecachebykeyprefix (String keyprefix)
{
System.Web.Caching.Cache _cache = Httpruntime.cache;
IDictionaryEnumerator cacheenum = _cache. GetEnumerator ();
ArrayList al = new ArrayList ();
while (Cacheenum.movenext ())
{
if (CacheEnum.Key.ToString (). StartsWith (Keyprefix))
{
Al. ADD (Cacheenum.key);
}
}

foreach (String key in AL)
{
_cache. Remove (key);
}
}

<summary>
Clear all cached content
</summary>
public static void Removeallcache ()
{
System.Web.Caching.Cache _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);
}
}
}

Common tool classes 8-cache classes

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.