. NET self-caching mechanism instances

Source: Internet
Author: User

Using System;
Using System.Web;
Using System.Web.Caching;
Using System.Collections.Generic;
Using System.Collections;

Namespace Szrpp.common
{
<summary>
Cache-related action classes
</summary>
public class Datacache
{
<summary>
Gets the cache value of the current application specified CacheKey
</summary>
<param name= "CacheKey" ></param>
<returns></returns>
public static Object GetCache (String CacheKey)
{
System.Web.Caching.Cache Objcache = Httpruntime.cache;
return Objcache[cachekey];
}

<summary>
Sets the cache value for the current application to specify CacheKey
</summary>
<param name= "CacheKey" ></param>
<param name= "Objobject" ></param>
public static void Setcache (String CacheKey, Object Objobject)
{
System.Web.Caching.Cache Objcache = Httpruntime.cache;
if (objobject!=null)
Objcache.insert (CacheKey, objobject, NULL, DateTime.UtcNow.AddDays (7), TimeSpan.Zero);
}

<summary>
Sets the cache value for the current application to specify CacheKey
</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>
Setting the cache based on dependencies
</summary>
<param name= "CacheKey" ></param>
<param name= "Objobject" ></param>
<param name= "CDEP" ></param>
<param name= "absoluteexpiration" ></param>
<param name= "slidingexpiration" ></param>
public static void Setcache (String CacheKey, Object Objobject, CacheDependency cdep, DateTime absoluteexpiration, Timespa n slidingexpiration)
{
System.Web.Caching.Cache Objcache = Httpruntime.cache;
Objcache.insert (CacheKey, Objobject, CDEP, absoluteexpiration, slidingexpiration);
}
<summary>
Removes the cache value for the specified cachekey of the current application
</summary>
<param name= "CacheKey" ></param>
public static void RemoveCache (String CacheKey)
{
System.Web.Caching.Cache Objcache = Httpruntime.cache;
IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator ();
while (enumerator. MoveNext ())
{
if (enumerator. Key.tostring (). IndexOf (CacheKey, System.StringComparison.Ordinal) >= 0)
Objcache.remove (Enumerator. Key.tostring ());
}

}
<summary>
Remove all caches from the current application
</summary>
public static void RemoveCache ()
{

Retrieve Application Cache Enumerator
IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator ();
Copy all keys this currently exist in Cache
while (enumerator. MoveNext ())
{
HttpRuntime.Cache.Remove (Enumerator. Key.tostring ());
}

}

}
}

. NET self-caching mechanism instances

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.