using System;
using System.Collections;
using System.Web;
using System.Web.Caching;
namespace Etmc.App.ETicket.Utility
{
/// <summary>
/// 缓存的工具类
/// </summary>
public class CacheUtility
{
/// <summary>
/// 用于缓存NULL数据
/// </summary>
public class CacheNull
{
private CacheNull(){}
private static CacheNull _instance = null;
/// <summary>
/// 得到唯一实例
/// </summary>
/// <returns></returns>
public static CacheNull Instance()
{
if(_instance == null)
{
_instance = new CacheNull();
}
return _instance;
}
}
private static Hashtable _CacheKey = new Hashtable();
private static string GetCacheKey(string strCacheKey,int intID)
{
return string.Format("CacheKey:{0}_ID:{1}",strCacheKey,intID);
}
private static string GetCacheKey(string strCacheKey,string[] parms)
{
if(parms == null || parms.Length == 0)
{
return string.Format("CacheKey:{0}",strCacheKey) + "_Parms:Null";
}
string strTemp = string.Format("CacheKey:{0}",strCacheKey);
for(int i=0;i<parms.Length;i++)
{
strTemp += string.Format("_Parm{0}:{1}",i,parms[i]);
}
return strTemp;
}
/// <summary>
/// 添加缓存 object有对应的ID
/// </summary>
/// <param name="objValue">缓存的值</param>
/// <param name="intID">主键ID</param>
/// <param name="strCacheKey">缓存关键字</param>
public static void AddCache(object objValue,int intID,string strCacheKey)
{
if(objValue == null)
{
objValue = CacheNull.Instance();
// if(GetCache(intID,strCacheKey) != null)
// {
// RemoveCache(intID,strCacheKey);
// }
// return;
}
if(GetCache(intID,strCacheKey)!=null)
{
return;
}
HttpRuntime.Cache.Add(GetCacheKey(strCacheKey,intID),objValue, null,
DateTime.Now.AddMinutes(Constant.CACHE_TIME), Cache.NoSlidingExpiration , CacheItemPriority.High, null);
ClearCache(strCacheKey);
}
/// <summary>
/// 得到缓存的信息
/// </summary>
/// <param name="intID">主键ID</param>
/// <param name="strCacheKey">缓存关键字</param>
/// <returns>缓存的对象</returns>
public static object GetCache(int intID,string strCacheKey)
{
object obj = GetCacheNull(intID,strCacheKey);
if(obj == CacheNull.Instance())
return null;
return obj;
}
/// <summary>
/// 得到缓存的信息 允许返回CacheNull
/// </summary>
/// <param name="intID">主键ID</param>
/// <param name="strCacheKey">缓存关键字</param>
/// <returns>缓存的对象</returns>
public static object GetCacheNull(int intID,string strCacheKey)
{
return HttpRuntime.Cache[GetCacheKey(strCacheKey,intID)];
}
/// <summary>
/// 移除缓存
/// </summary>
/// <param name="intID">主键ID</param>
/// <param name="strCacheKey">缓存关键字</param>
public static void RemoveCache(int intID,string strCacheKey)
{
string strKey = GetCacheKey(strCacheKey,intID);
if(HttpRuntime.Cache[strKey]!=null)
{
HttpRuntime.Cache.Remove(strKey);
}
ClearCache(strCacheKey);
}
private static void ClearCache(string strCacheKey)
{
//当单个的更改时,搜索结果也失效
ArrayList alKeys = _CacheKey[strCacheKey] as ArrayList;
if(alKeys!=null)
{
for(int i=0;i<alKeys.Count;i++)
{
RemoveCache(strCacheKey,(string[])alKeys[i]);
}
}
_CacheKey[strCacheKey] = null;
}
/// <summary>
/// 添加缓存 object为搜索的结果
/// </summary>
/// <param name="objValue">缓存的值</param>
/// <param name="strCacheKey">缓存关键字</param>
/// <param name="strParms">参数数组</param>
public static void AddCache(object objValue,string strCacheKey,string[] Parms)
{
lock(typeof(CacheUtility))
{
if(objValue == null)
{
objValue = CacheNull.Instance();
// if(GetCache(strCacheKey,Parms)!=null)
// {
// RemoveCache(strCacheKey,Parms);
// }
// return;
}
if(GetCache(strCacheKey,Parms)!=null)
{
return;
}
if(_CacheKey[strCacheKey] == null)
{
_CacheKey[strCacheKey] = new ArrayList();
}
ArrayList alKeys = (ArrayList) _CacheKey[strCacheKey];
if(!alKeys.Contains(Parms))
{
alKeys.Add(Parms);
}
HttpRuntime.Cache.Add(GetCacheKey(strCacheKey,Parms),objValue, null,
DateTime.Now.AddMinutes(Constant.CACHE_TIME), Cache.NoSlidingExpiration , CacheItemPriority.High, null);
}
}
/// <summary>
/// 得到缓存的信息
/// </summary>
/// <param name="strCacheKey">缓存关键字</param>
/// <param name="Parms">参数数组</param>
/// <returns>缓存的对象</returns>
public static object GetCache(string strCacheKey,string[] Parms)
{
object obj = GetCacheNull(strCacheKey,Parms);
if(obj == CacheNull.Instance())
return null;
return obj;
}
/// <summary>
/// 得到缓存的信息 允许返回CacheNull
/// </summary>
/// <param name="strCacheKey">缓存关键字</param>
/// <param name="Parms">参数数组</param>
/// <returns>缓存的对象</returns>
public static object GetCacheNull(string strCacheKey,string[] Parms)
{
return HttpRuntime.Cache[GetCacheKey(strCacheKey,Parms)];
}
/// <summary>
/// 移除缓存
/// </summary>
/// <param name="strCacheKey">缓存关键字</param>
/// <param name="Parms">参数数组</param>
private static void RemoveCache(string strCacheKey,string[] Parms)
{
string strKey = GetCacheKey(strCacheKey,Parms);
if(HttpRuntime.Cache[strKey]!=null)
{
HttpRuntime.Cache.Remove(strKey);
}
}
}
}