using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;
using System.Web.Caching;
namespace yang.Utility
{
public static class CacheHelper
{
static Cache WebCache;
static HWCache TempCache=new HWCache ();
static bool IsWebCacheGet()
{
if (WebCache == null)
{
if (HttpContext.Current != null && HttpContext.Current.Cache != null)
{
WebCache = HttpContext.Current.Cache;
return true;
}
else
return false;
}
return true;
}
public static void Insert(string CacheName,Object CacheObject,int CacheMinutes)
{
if (!string.IsNullOrEmpty(CacheName) && CacheObject!=null)
if (IsWebCacheGet())
WebCache.Insert(CacheName, CacheObject, null, DateTime.Now.AddMinutes(CacheMinutes), System.Web.Caching.Cache.NoSlidingExpiration);
else
{
TempCache.Insert(CacheName, CacheObject, CacheMinutes);
}
}
public static object Get(string CacheName)
{
object CacheObject=null;
if (!string.IsNullOrEmpty(CacheName) )
if (IsWebCacheGet())
CacheObject = WebCache[CacheName];
else
{
CacheObject = TempCache.Get (CacheName);
}
return CacheObject;
}
public static void Remove(string CacheName)
{
if (!string.IsNullOrEmpty(CacheName))
if (IsWebCacheGet())
WebCache.Remove(CacheName);
else
{
TempCache.Remove(CacheName);
}
}
public static int Clear(string CachePreStr)
{
int CacheCount = 0;
if (IsWebCacheGet())
{
CacheCount = WebCache.Count;
IDictionaryEnumerator CacheEnum = WebCache.GetEnumerator();
while (CacheEnum.MoveNext())
{
WebCache.Remove(CacheEnum.Key.ToString());
}
TempCache.Clear();
}
else
{
CacheCount = TempCache.Clear();
}
string TempLog = "清空了" + CacheCount.ToString() + "条缓冲记录";
LogHelper.Warn("CacheHelper", TempLog);
return CacheCount;
}
}
/// <summary>
/// 自定义缓存
/// </summary>
public class HWCache:IDisposable
{
static List<HWCache> CacheList = new List<HWCache>();
/// <summary>
/// 缓存Hash
/// </summary>
Hashtable CacheObjectHash = new Hashtable();
/// <summary>
/// 缓存时间Hash
/// </summary>
Hashtable CacheTimeHash = new Hashtable();
/// <summary>
/// 自动移除
/// </summary>
bool AutoRemove = true;
/// <summary>
/// 自定义缓存
/// </summary>
public HWCache()
{
this.AutoRemove = true;
CacheList.Add(this);
}
/// <summary>
/// 自定义缓存
/// </summary>
/// <param name="AutoRemove">自动移除</param>
public HWCache(bool AutoRemove)
{
this.AutoRemove = AutoRemove;
CacheList.Add(this);
}
public void Insert(string CacheName, object CacheObject, int CacheMinutes)
{
CacheObjectHash.Remove(CacheName);
CacheTimeHash.Remove(CacheName);
CacheObjectHash.Add(CacheName, CacheObject);
CacheTimeHash.Add(CacheName, DateTime.Now.AddMinutes(CacheMinutes));
}
public object Get(string CacheName)
{
return Get(CacheName, true);
}
/// <summary>
/// 获取缓存数据
/// </summary>
/// <param name="CacheName">缓存名称</param>
/// <param name="TimeLimit">是否采用时间限制,默认采用,不采用只有在AutoRemove为Fasle才有效果</param>
/// <returns></returns>
public object Get(string CacheName,bool TimeLimit)
{
object CacheObject = CacheObjectHash[CacheName];
if (CacheObject != null&&TimeLimit )
{
DateTime CacheLimit = (DateTime)(CacheTimeHash[CacheName]);
if (CacheLimit < DateTime.Now)
{
CacheObject = null;
if (AutoRemove)
Remove(CacheName);
}
}
return CacheObject;
}
public void Remove(string CacheName)
{
CacheObjectHash.Remove(CacheName);
CacheTimeHash.Remove(CacheName);
}
public int Clear()
{
int Count=CacheObjectHash.Count;
CacheObjectHash.Clear();
CacheTimeHash.Clear();
return Count;
}
#region IDisposable 成员
public void Dispose()
{
CacheList.Remove(this);
}
#endregion
}
}