using System;
using System.Web;
using System.Web.Caching;
using System.Collections;
namespace WebApplication1
{
/// <summary>
/// 缓存的类型,他们之间是否是包含关系?
/// </summary>
public enum CoreCacheType
{
Host = 1,
Portal,
Tab
}
/// <summary>
/// 对系统缓存的操作
/// </summary>
public class DataCache
{
public DataCache()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 取得某缓存的值
/// </summary>
/// <param name="CacheKey">缓存的键</param>
/// <returns>缓存的值</returns>
public static object GetCache(string CacheKey)
{
// HttpRuntime.Cache:获取当前应用程序的 System.Web.Caching.Cache。
// System.Web.Caching.Cache:实现用于 Web 应用程序的缓存。
Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置某缓存的值。
/// </summary>
/// <param name="CacheKey">缓存的键</param>
/// <param name="objObject">缓存的值</param>
public static void SetCache(string CacheKey, object objObject)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 设置某缓存的值和跟踪缓存依赖项。
/// </summary>
/// <param name="CacheKey">缓存的键</param>
/// <param name="objObject">缓存的值</param>
/// <param name="objDependency">缓存依赖项</param>
public static void SetCache(string CacheKey, object objObject, CacheDependency objDependency)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, objDependency);
}
/// <summary>
/// 设置某缓存的值和跟踪缓存依赖项。
/// </summary>
/// <param name="CacheKey">缓存的键</param>
/// <param name="objObject">缓存的值</param>
/// <param name="objDependency">缓存依赖项</param>
/// <param name="AbsoluteExpiration">缓存移出的时间</param>
/// <param name="SlidingExpiration">缓存的有效期时间长度</param>
public static void SetCache(string CacheKey, object objObject, CacheDependency objDependency, DateTime AbsoluteExpiration, TimeSpan SlidingExpiration)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, objDependency, AbsoluteExpiration, SlidingExpiration);
}
/// <summary>
/// 设置某缓存的值。
/// </summary>
/// <param name="CacheKey">缓存的键</param>
/// <param name="objObject">缓存的值</param>
/// <param name="SlidingExpiration">缓存的有效期时间长度</param>
public static void SetCache(string CacheKey, object objObject, TimeSpan SlidingExpiration)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, Cache.NoAbsoluteExpiration, SlidingExpiration);
}
/// <summary>
/// 设置某缓存的值。
/// </summary>
/// <param name="CacheKey">缓存的键</param>
/// <param name="objObject">缓存的值</param>
/// <param name="AbsoluteExpiration">缓存移出的时间</param>
public static void SetCache(string CacheKey, object objObject, DateTime AbsoluteExpiration)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, AbsoluteExpiration, Cache.NoSlidingExpiration);
}
/// <summary>
/// 从缓存中移出某键的缓存值
/// </summary>
/// <param name="CacheKey">缓存的键</param>
public static void RemoveCache(string CacheKey)
{
Cache objCache = HttpRuntime.Cache;
if (objCache[CacheKey] != null)
{
objCache.Remove(CacheKey);
}
}
public static void ClearCoreCache(CoreCacheType Type, int ID, bool Cascade)
{
switch (Type)
{
// 清除Host缓存
case CoreCacheType.Host:
ClearHostCache(Cascade);
break;
// 清除Portal缓存
case CoreCacheType.Portal:
ClearPortalCache(ID, Cascade);
break;
// 清除Tab缓存
case CoreCacheType.Tab:
ClearTabCache(ID);
break;
default:
break;
}
}
/// <summary>
/// 清除Host缓存。
/// </summary>
/// <param name="Cascade"></param>
private static void ClearHostCache(bool Cascade)
{
Cache objCache = HttpRuntime.Cache;
if (objCache["GetHostSettings"] != null)
{
objCache.Remove("GetHostSettings");
}
if (objCache["GetPortalByAlias"] != null)
{
objCache.Remove("GetPortalByAlias");
}
if (objCache["CSS"] != null)
{
objCache.Remove("CSS");
}
if (Cascade)
{
PortalController objPortals = new PortalController();
// 在这里声明PortalInfo,是为了提高效率考虑。
PortalInfo objPortal;
// 取得一个PortalInfo集合
ArrayList arrPortals = objPortals.GetPortals();
for (int intIndex = 0; intIndex < arrPortals.Count; intIndex++)
{
objPortal = (PortalInfo)arrPortals[intIndex];
// 清除Portal缓存
ClearPortalCache(objPortal.PortalID, Cascade);
}
}
}
/// <summary>
/// 清除Portal缓存。
/// </summary>
/// <param name="PortalId"></param>
/// <param name="Cascade"></param>
private static void ClearPortalCache(int PortalId, bool Cascade)
{
Cache objCache = HttpRuntime.Cache;
if (objCache["GetPortalSettings" + PortalId] != null)
{
objCache.Remove("GetPortalSettings" + PortalId);
}
if (objCache["GetTabs" + PortalId] != null)
{
objCache.Remove("GetTabs" + PortalId);
}
if (Cascade)
{
TabController objTabs = new TabController();
TabInfo objTab;
// 取得TabInfo集合
ArrayList arrTabs = objTabs.GetTabs(PortalId);
for (int intIndex = 0; intIndex < arrTabs.Count; intIndex++)
{
objTab = (TabInfo)arrTabs[intIndex];
// 移除Tab缓存
ClearTabCache(objTab.TabID);
}
}
}
/// <summary>
/// 清除Tab缓存。
/// </summary>
/// <param name="TabId"></param>
private static void ClearTabCache(int TabId)
{
Cache objCache = HttpRuntime.Cache;
if (objCache["GetTab" + TabId] != null)
{
objCache.Remove("GetTab" + TabId);
}
if (objCache["GetPortalTabModules" + TabId] != null)
{
objCache.Remove("GetPortalTabModules" + TabId);
}
TabController objTabs = new TabController();
// 取得TabInfo对象
TabInfo objTab = objTabs.GetTab(TabId);
if (objTab != null)
{
if (objCache["GetTabPermissionsByPortal" + objTab.PortalID] != null)
{
objCache.Remove("GetTabPermissionsByPortal" + objTab.PortalID);
}
}
}
}
}
-------------------
另一种:
public class DataCache
{
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration,TimeSpan slidingExpiration )
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject,null,absoluteExpiration,slidingExpiration);
}
}