62,046
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 网站全局缓存类(可以封装于DLL)
/// </summary>
public class Cache
{
/// <summary>
/// 强制清除所有服务端此程序产生的缓存
/// 可能对同时在线的其他用户造成数据丢失风险
/// </summary>
public static void Dispose()
{
HttpContext.Current.Application["Session"] = null;
}
public static void Clear()
{
Hashtable SessionTab = new Hashtable();
if (HttpContext.Current.Application["Session"] != null)
{
SessionTab = HttpContext.Current.Application["Session"] as Hashtable;
Hashtable VTab = new Hashtable();
ArrayList DeleteKey = new ArrayList();
//HttpContext.Current.Response.Write("总计:" + SessionTab.Count + "<br/>");
foreach (System.Collections.DictionaryEntry Obj in SessionTab)
{
//HttpContext.Current.Response.Write(Obj.Key + "<br/>");
VTab = Obj.Value as Hashtable;
// 获取时间
// HttpContext.Current.Response.Write(VTab["创建时间"] + "<br/>");
DateTime Old_Time = (DateTime)VTab["创建时间"];
double Over_Time = (double)VTab["过期时间"];
DateTime Now_Time = DateTime.Now;
//HttpContext.Current.Response.Write(Old_Time + "<br/>");
//// 检查过期
if ((Now_Time - Old_Time).TotalMilliseconds >= Over_Time)
{
DeleteKey.Add(Obj.Key);
//HttpContext.Current.Response.Write(Obj.Key + "超时<br/>");
}
}
for (int i = 0; i < DeleteKey.Count; i++)
{
SessionTab.Remove(DeleteKey[i]);
//HttpContext.Current.Response.Write(DeleteKey[i] + "被清理了<br/>");
}
}
}
/// <summary>
/// 注销并清除当前线程下用户的缓存数据
/// </summary>
/// <param name="Key"></param>
public static void Dispose(object Key)
{
if (Key != null)
{
Hashtable SessionTab = new Hashtable();
if (HttpContext.Current.Application["Session"] != null)
{
SessionTab = HttpContext.Current.Application["Session"] as Hashtable;
if (SessionTab.ContainsKey(Key))//若包含,则更新
{
SessionTab.Remove(Key);
}
}
HttpContext.Current.Application["Session"] = SessionTab;
}
}
/// <summary>
/// 设置(同时起到刷新值的作用)
/// </summary>
/// <param name="Key"></param>
/// <param name="Value"></param>
public static void Init(object Key, object Value)
{
double OverTime = 1000 * 10;
// 毫秒 -- 10 秒(默认)
//10秒过期,你可以设置的更长,也可以加入(Init)参数自行设定
if (Key != null)
{
Hashtable SessionTab = new Hashtable();
Hashtable MyValueTab = new Hashtable();
if (HttpContext.Current.Application["Session"] != null)
{
SessionTab = HttpContext.Current.Application["Session"] as Hashtable;
if (SessionTab.ContainsKey(Key))
{
// 存在 修改
MyValueTab = SessionTab[Key] as Hashtable;
MyValueTab[Key] = Value;
SessionTab[Key] = MyValueTab;
}
else
{
// 若有其他信息,再此处添加
MyValueTab.Add(Key, Value);
MyValueTab.Add("创建时间", DateTime.Now);
MyValueTab.Add("过期时间", OverTime);
SessionTab.Add(Key, MyValueTab);
}
}
else
{
// 若有其他信息,再此处添加
MyValueTab.Add(Key, Value);
MyValueTab.Add("创建时间", DateTime.Now);
MyValueTab.Add("过期时间", OverTime);
SessionTab.Add(Key, MyValueTab);
//不存在,首次添加
}
HttpContext.Current.Application["Session"] = SessionTab;
}
Clear(); // 执行后自动清理过期缓存,词句可以在程序内引用
}
/// <summary>
/// 取值
/// </summary>
/// <param name="Key"></param>
/// <returns></returns>
public static object Value(object Key)
{
if (Key != null)
{
Hashtable SessionTab = new Hashtable();
Hashtable MyValueTab = new Hashtable();
if (HttpContext.Current.Application["Session"] != null)
{
SessionTab = HttpContext.Current.Application["Session"] as Hashtable;
if (SessionTab.ContainsKey(Key))
{
MyValueTab = SessionTab[Key] as Hashtable;
return MyValueTab[Key];
}
else
{
return null;
}
}
else
{
return null;
}
}
else
{
return null;
}
Clear(); // 执行后自动清理过期缓存
}
}