asp.net 自己写的一个序列化缓存类

2 篇文章 0 订阅
2 篇文章 0 订阅

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 = "";
        }


    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值