using System;
using System.Web;
using System.Web.Caching;
namespace FHT.Agency.Web.Utility
{
public sealed class CacheHelper
{
///
/// 获取数据缓存
///
/// 键
public static string GetCache(string CacheKey)
{
Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey]!= null? objCache[CacheKey].ToString() :"";
}
///
/// 设置数据缓存
///
public static void SetCache(string CacheKey, object objObject)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
///
/// 设置数据缓存
///
public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, CacheItemPriority.NotRemovable, null);
}
///
/// 设置数据缓存
///
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
///
/// 移除指定数据缓存
///
public static void RemoveAllCache(string CacheKey)
{
Cache _cache = HttpRuntime.Cache;
_cache.Remove(CacheKey);
}
///
/// 移除全部缓存
///
public static void RemoveAllCache()
{
Cache _cache = HttpRuntime.Cache;
System.Collections.IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
_cache.Remove(CacheEnum.Key.ToString());
}
}
}
}