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