首页 > 代码库 > cache使用
cache使用
public class CacheHelper
{
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());
}
}
{
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;
}
cache使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。