网页 缓存

掉用缓存方法:

 var listModel =ProclamationCache.GetProclamationList();

//实现方法

public static List<ProclamationModel> GetProclamationList()
{
var cache = GetTopProclamationList();
if (cache != null)
{
return cache;
}
return null;
}

//实现调用方法

private static List<ProclamationModel> GetTopProclamationList()
{
string CacheKey_ProList = "_CacheKey_ProList";
int CacheOutTime = ConfigHelperBLL.CacheOutTime;

List<ProclamationModel> listModel = HttpRuntime.Cache[CacheKey_ProList] as List<ProclamationModel>;
if (listModel == null)
{
listModel = GetProclamationList_ForCache(0, 30);
HttpRuntime.Cache.Insert(CacheKey_ProList, listModel, null,
DateTime.Now.AddSeconds(CacheOutTime), System.Web.Caching.Cache.NoSlidingExpiration, GetTopProclamationList_UpdateCallback);
}
return listModel;
}

//实现调用方法

private static void GetTopProclamationList_UpdateCallback(string key, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration)
{
int CacheOutTime = ConfigHelperBLL.CacheOutTime;
expensiveObject = GetProclamationList_ForCache(0, 30);
dependency = null;
absoluteExpiration = DateTime.Now.AddSeconds(CacheOutTime);
slidingExpiration = Cache.NoSlidingExpiration;
}

//实现调用方法

public static int CacheOutTime
{
get
{
int time = 0;
if(!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["CacheOutTime"],out time))
{
time = 3;
}
return time;
}
}

posted @ 2016-03-03 15:23  张三的编码生活  阅读(137)  评论(0编辑  收藏  举报