using System;
using System.Web;
using System.Text;
using System.IO;
using dou7.Common;
using dou7.XML;
namespace dou7.Web.Common
{
public class WebCache
{
public static string CahceFile = "";
public static string SessionID
{
get
{
return HttpContext.Current.Session.SessionID;
}
}
static WebCache()
{
if (CahceFile == "")
{
CahceFile = HttpContext.Current.Server.MapPath("~") + @"Data\Cache.xml";
}
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="name">缓存项名称</param>
/// <param name="obj">缓存内容</param>
public static void SetCache(string name, object obj)
{
byte[] cache = dou7.Common.Common.Serialize(obj);
XMLHelper xh = new XMLHelper(CahceFile);
if (!xh.Contains(SessionID))
{
xh.Add(SessionID, "").Add(name, dou7.Common.Common.BytesToBase64(cache));
return;
}
if (!xh[SessionID].Contains(name))
{
xh[SessionID].Add(name, dou7.Common.Common.BytesToBase64(cache));
}
else
{
xh[SessionID][name].Value = dou7.Common.Common.BytesToBase64(cache);
}
}
/// <summary>
/// 获取指定缓存内容
/// </summary>
/// <param name="name">缓存项名称</param>
/// <returns>缓存内容</returns>
public static object GetCache(string name)
{
XMLHelper xh = new XMLHelper(CahceFile);
string content = xh[SessionID + "/" + name].Value;
if (content != "")
{
byte[] cache = dou7.Common.Common.Base64ToBytes(content);
object obj = dou7.Common.Common.Deserialize(cache);
return obj;
}
else
{
return null;
}
}
/// <summary>
/// 清理当前会话缓存
/// </summary>
public static void Clear()
{
XMLHelper xh = new XMLHelper(CahceFile);
if (xh.Exists(SessionID))
{
xh.Remove(SessionID);
}
}
/// <summary>
/// 清理指定会话ID
/// </summary>
/// <param name="sessionID"></param>
public static void Clear(string sessionID)
{
XMLHelper xh = new XMLHelper(CahceFile);
if (xh.Contains(sessionID))
{
xh.Remove(sessionID);
}
}
/// <summary>
/// 清理所有网页缓存
/// </summary>
public static void ClearAll()
{
XMLHelper xh = new XMLHelper(CahceFile);
xh.Value = "";
}
}
}