HttpCache Cache extension Methods

Source: Internet
Author: User

Using System;
Using System.Collections;
Using System.Configuration;
Using System.Web;
Using System.Web.Caching;

Namespace Meb.Common.Extensions
{
Cache Write
Verify that the cache exists
Cache reads read the corresponding cache values by cache index
public static Class Cacheextension
{
private static int cacheminute = Int. Parse (configurationmanager.appsettings["Cacheminute"]?? "30");

//<summary>
//Get the cache value of the current application specified CacheKey
//</summary>
//<param name= "CacheKey" ;</param>
//<returns></returns>
public static Object GetCache (String cacheKey)
{
VA R Objcache = Httpruntime.cache;
return Objcache[cachekey];
}
//<summary>
//query Specifies whether the cache value for CacheKey exists
//</summary>
//<param name= "Cacheke Y "></param>
//<returns></returns>
public static bool Isexistscache (string CacheKey)
{
var objcache = Httpruntime.cache;
Return objcache[cachekey]! = null;
}

<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)
{
var objcache = Httpruntime.cache;
Objcache.insert (CacheKey, objobject);
}

<summary>
Sets the cache value for the current application to specify CacheKey
</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)
{
var objcache = Httpruntime.cache;
Objcache.insert (CacheKey, objobject, NULL, absoluteexpiration, slidingexpiration);
}
<summary>
Setting up the data cache
</summary>
public static void Setcache (String CacheKey, Object Objobject, TimeSpan timeout)
{
var objcache = Httpruntime.cache;
Objcache.insert (CacheKey, objobject, NULL, DateTime.MaxValue, timeout, cacheitempriority.notremovable, null);
}
<summary>
Remove the specified data cache
</summary>
public static void Removeallcache (String cacheKey)
{
var cache = Httpruntime.cache;
Cache. Remove (CacheKey);
}
<summary>
Removing all Caches
</summary>
public static void Removeallcache ()
{
var cache = Httpruntime.cache;
var cacheenum = cache. GetEnumerator ();
while (Cacheenum.movenext ())
{
Cache. Remove (CacheEnum.Key.ToString ());
}
}

private static int cacheminute = Int. Parse (configurationmanager.appsettings["Cacheminute"]?? "30");
Private Const string Applicationcachekeyformat = "application_{0}";
Private Const string Userinfocachekeyformat = "userinfo_{0}";
Private Const string Rolepopedomcachekeyformat = "rolepopedom_{0}";

public static void Insertforslidingexpiration (this cache cache, string key, Object value)
{
Cache. Insert (key, value, NULL, Cache.noabsoluteexpiration, Timespan.fromminutes (Cacheminute));
}

public static T-get<t> (this cache cache, Object Datacachekey, func<object[], t> getService) where T:class
{
var CacheKey = string. Format ("{0}_{1}", typeof (T). FullName, Datacachekey);
var data = cache. Get (CacheKey) as T;
if (data! = NULL)
{
return data;
}
data = GetService (new object[] {datacachekey});
if (data! = NULL)
{
Cache. Insertforslidingexpiration (CacheKey, data);
return data;
}
return null;
}

public static void Remove<t> (this cache cache, object Datacachekey)
{
var CacheKey = string. Format ("{0}_{1}", typeof (T). FullName, Datacachekey);
Cache. Remove (CacheKey);
}
}
}

HttpCache Cache extension Methods

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.