清空对象缓存:
CacheHandler.Write("Account_users_" + UserExt.UserID, null);
清空集合缓存
CacheHandler.Write("AllAccount_RegionsIist" , null);
读取缓存集合:
#region 读取所有省市区
/// <summary>
/// 读取所有省市区
/// </summary>
/// <returns></returns>
public List<IQingWei.SysManage.Model.Account_Regions> GetAllRegions()
{
List<IQingWei.SysManage.Model.Account_Regions> RegionsList =CacheHandler.Read("AllAccount_RegionsIist") as List<IQingWei.SysManage.Model.Account_Regions>;
if (RegionsList == null)
{
RegionsList =Account_RegionsBLL.GetModelList(null, null);
CacheHandler.Write("AllAccount_RegionsIist", RegionsList);
}
return RegionsList;
}
#endregion
读取缓存对象:
#region 获取单个缓存
public IQingWei.SysManage.Model.Account_Users GetUsers(int UserID)
{
IQingWei.SysManage.Model.Account_Users _Account_Users = CacheHandler.Read("Account_users_" + UserID) as IQingWei.SysManage.Model.Account_Users;
if (_Account_Users == null)
{
_Account_Users = Account_UsersBLL.GetModel(int.Parse(jObject["UserID"].ToString()));
}
}
#endregion
缓存类的代码:
/// <summary>
/// 缓存专用类
/// </summary>
public static class CacheHandler
{
/// <summary>
/// 填充缓存
/// </summary>
/// <param name="cacheID">缓存名称</param>
/// <param name="data">魂村内容</param>
/// <returns></returns>
public static bool Write(string cacheID, object data)
{
if (HttpContext.Current == null)
return false;
if (cacheID == null || cacheID.Equals(""))
return false;
HttpRuntime.Cache.Insert(
cacheID, data, null, Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null
);
return true;
}
/// <summary>
/// 填充缓存
/// </summary>
/// <param name="cacheID">缓存名称</param>
/// <param name="data">缓存内容</param>
/// <param name="Ts">到期时间</param>
/// <returns></returns>
public static bool Write(string cacheID, object data,TimeSpan Ts)
{
if (HttpContext.Current == null)
return false;
if (cacheID == null || cacheID.Equals(""))
return false;
HttpRuntime.Cache.Insert(cacheID, data, null, Cache.NoAbsoluteExpiration,Ts, CacheItemPriority.NotRemovable, null);
return true;
}
/// <summary>
/// 读取缓存
/// </summary>
/// <param name="cacheID">缓存名称</param>
/// <returns></returns>
public static object Read(string cacheID)
{
if (HttpContext.Current == null)
return null;
return HttpRuntime.Cache.Get(cacheID);
}
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="cacheID"></param>
public static void Remove(string cacheID)
{
if (HttpContext.Current == null)
return;
if (cacheID == null || cacheID.Equals(""))
return;
HttpRuntime.Cache.Remove(cacheID);
}
}