C # Cache Cache read and take settings

CacheHelper.cs create a class, as follows:


the System the using;
the using the System.Web;
the using the System.Collections;
the using the System.Web.Caching;

public class CacheHelper
{
/// <Summary>
/// Get data cache
/// </ Summary>
/// <param name = "cacheKey"> key </ param>
public static Object GetCache (String cacheKey)
{
var objCache = HttpRuntime.Cache.Get (cacheKey);
return objCache;
}
/// <Summary>
/// set the data cache
/// < / Summary>
public static void setCache (cacheKey String, Object objObject)
{
var = objCache the HttpRuntime.Cache;
objCache.Insert (cacheKey, objObject);
}
/// <Summary>
///-data cache
/// </ summary >
static void setCache public (cacheKey String, Object objObject, int timeout = 7200)
{
the try
{
IF (objObject == null) return;
var = objCache the HttpRuntime.Cache;
// relative expiry
//objCache.Insert(cacheKey, objObject, null , DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
// absolute expiration time
objCache.Insert (cacheKey, objObject, null, DateTime.Now.AddSeconds (timeout), TimeSpan.Zero, CacheItemPriority.High, null);
}
the catch (Exception)
{
// the throw;
}
}
/// <Summary>
/// Removes the specified data buffer
/// </ Summary>
public static void RemoveAllCache (String cacheKey)
{
var = the HttpRuntime.Cache cache;
cache.Remove(cacheKey);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
var cache = HttpRuntime.Cache;
var cacheEnum = cache.GetEnumerator();
while (cacheEnum.MoveNext())
{
cache.Remove(cacheEnum.Key.ToString());
}
}
}

References are also attached to the top, and so few.
Then call:

 


the IEnumerable public <CompanyModel> FindCompanys ()
{
var = CacheHelper.GetCache Cache ( "commonData_Company"); // first read
if (cache == null) // if the cache is not
{
var queryCompany _base.CompanyModel = (); / / removed from the database
var = queryCompany.ToList Enumerable ();
CacheHelper.SetCache ( "commonData_Company", Enumerable); // add the cache
return Enumerable;
}
var Result = (List <CompanyModel>) cache; // returns directly to have the cache
return Result;
}

The test results also Tieshanglai look good:


First came a load is null, then read the database, add the cache, the current return to the front desk is removed from the data in the database.

 

Refresh the page and found the cache already has 30 data read out,

 

Then the next to go, return data cache:

 

Guess you like

Origin www.cnblogs.com/yatai-bd/p/12114990.html
Recommended