cache使用
{
private static object objCacheItem = new object();
public static void SetCache(string key, object value, int second)
{
lock (objCacheItem)
{
HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddSeconds(second * 1000), System.Web.Caching.Cache.NoSlidingExpiration);
}
}
public static object GetCache(string key)
{
if (string.IsNullOrEmpty(key))
return null;
return HttpRuntime.Cache[key];
}
public static void RemoveCache(string key)
{
if (GetCache(key) != null)
{
lock (objCacheItem)
{
HttpRuntime.Cache.Remove(key);
}
}
}
public static void RemoveMultiCache(string keyInclude)
{
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
if (string.IsNullOrEmpty(keyInclude))
{
RemoveCache(CacheEnum.Key.ToString());
continue;
}
if (CacheEnum.Key.ToString().IndexOf(keyInclude) >= 0)
RemoveCache(CacheEnum.Key.ToString());
}
}
}
var key = "DATADICTIONARIES_" + typeId;
var obj = JCK.Helper.CacheHelper.GetCache(key);
if (obj != null)
return (List<JCK.DAL.DataDictionary>)obj;
using (var entity = new JCK.DAL.Entities())
{
var list = entity.DataDictionaries.Where(m => m.TypeId == typeId && m.Isvalid).ToList();
if (list != null)
JCK.Helper.CacheHelper.SetCache(key, list, 60 * 60);
return list;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。