ASP.NET操作cookie、session、cache工具类

public static class Cookie
    {
        /// 创建COOKIE对象并赋Value值
        ///


        /// 创建COOKIE对象并赋Value值,修改COOKIE的Value值也用此方法,因为对COOKIE修改必须重新设Expires
        ///

        /// COOKIE对象名
        /// COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),
        /// COOKIE对象Value值
        public static void SetObj(string strCookieName, int iExpires, string strValue)
        {
            HttpCookie bjCookie = new HttpCookie(strCookieName.Trim());
            objCookie.Value = Utils.EncodeURIComponent(strValue.Trim());
            objCookie.Domain = N0.Config.CommonConfig.strDomain;
            if (iExpires > 0)
            {
                if (iExpires == 1)
                {
                    objCookie.Expires = DateTime.MaxValue;
                }
                else
                {
                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
                }
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }
        /// 创建COOKIE对象并赋多个KEY键值
        ///
        /// 创建COOKIE对象并赋多个KEY键值
        /// 设键/值如下:
        /// NameValueCollection myCol = new NameValueCollection();
        /// myCol.Add("red", "rojo");
        /// myCol.Add("green", "verde");
        /// myCol.Add("blue", "azul");
        /// myCol.Add("red", "rouge");   结果“red:rojo,rouge;green:verde;blue:azul”
        ///

        /// COOKIE对象名
        /// COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),
        /// 键/值对集合
        public static void SetObj(string strCookieName, int iExpires, NameValueCollection KeyValue)
        {
            HttpCookie bjCookie = new HttpCookie(strCookieName.Trim());
            foreach (String key in KeyValue.AllKeys)
            {
                objCookie[key] = Utils.EncodeURIComponent(KeyValue[key].Trim());
            }
            objCookie.Domain = N0.Config.CommonConfig.strDomain;
            if (iExpires > 0)
            {
                if (iExpires == 1)
                {
                    objCookie.Expires = DateTime.MaxValue;
                }
                else
                {
                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
                }
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }
        /// 创建COOKIE对象并赋Value值
        ///
        /// 创建COOKIE对象并赋Value值,修改COOKIE的Value值也用此方法,因为对COOKIE修改必须重新设Expires
        ///

        /// COOKIE对象名
        /// COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),
        /// 作用域
        /// COOKIE对象Value值
        public static void SetObj(string strCookieName, int iExpires, string strValue, string strDomain)
        {
            HttpCookie bjCookie = new HttpCookie(strCookieName.Trim());
            objCookie.Value = Utils.EncodeURIComponent(strValue.Trim());
            objCookie.Domain = strDomain.Trim();
            if (iExpires > 0)
            {
                if (iExpires == 1)
                {
                    objCookie.Expires = DateTime.MaxValue;
                }
                else
                {
                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
                }
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }
        /// 创建COOKIE对象并赋多个KEY键值
        ///
        /// 创建COOKIE对象并赋多个KEY键值
        /// 设键/值如下:
        /// NameValueCollection myCol = new NameValueCollection();
        /// myCol.Add("red", "rojo");
        /// myCol.Add("green", "verde");
        /// myCol.Add("blue", "azul");
        /// myCol.Add("red", "rouge");   结果“red:rojo,rouge;green:verde;blue:azul”
        ///

        /// COOKIE对象名
        /// COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365),
        /// 作用域
        /// 键/值对集合
        public static void SetObj(string strCookieName, int iExpires, NameValueCollection KeyValue, string strDomain)
        {
            HttpCookie bjCookie = new HttpCookie(strCookieName.Trim());
            foreach (String key in KeyValue.AllKeys)
            {
                objCookie[key] = Utils.EncodeURIComponent(KeyValue[key].Trim());
            }
            objCookie.Domain = strDomain.Trim();
            if (iExpires > 0)
            {
                if (iExpires == 1)
                {
                    objCookie.Expires = DateTime.MaxValue;
                }
                else
                {
                    objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
                }
            }
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }

        /// 读取Cookie某个对象的Value值
        ///


        /// 读取Cookie某个对象的Value值,返回Value值,如果对象本就不存在,则返回字符串"CookieNonexistence"
        ///

        /// Cookie对象名称
        /// Value值,如果对象本就不存在,则返回字符串"CookieNonexistence"
        public static string GetValue(string strCookieName)
        {
            if (HttpContext.Current.Request.Cookies[strCookieName] == null)  
              {
                  return "CookieNonexistence";
              }  
              else  
              {
                  return Utils.DecodeURIComponent(HttpContext.Current.Request.Cookies[strCookieName].Value);
              }
        }
        /// 读取Cookie某个对象的某个Key键的键值
        ///
        /// 读取Cookie某个对象的某个Key键的键值,返回Key键值,如果对象本就不存在,则返回字符串"CookieNonexistence",如果Key键不存在,则返回字符串"KeyNonexistence"
        ///

        /// Cookie对象名称
        /// Key键名
        /// Key键值,如果对象本就不存在,则返回字符串"CookieNonexistence",如果Key键不存在,则返回字符串"KeyNonexistence"
        public static string GetValue(string strCookieName,string strKeyName)
        {
            if (HttpContext.Current.Request.Cookies[strCookieName] == null)
            {
                return "CookieNonexistence";
            }
            else
            {
                string strObjValue = Utils.DecodeURIComponent(HttpContext.Current.Request.Cookies[strCookieName].Value);
                string strKeyName2 = strKeyName+"=";
                if (strObjValue.IndexOf(strKeyName2) == -1)
                {
                    return "KeyNonexistence";
                }
                else
                {
                    return Utils.DecodeURIComponent(HttpContext.Current.Request.Cookies[strCookieName][strKeyName]);
                }
            }
        }

        /// 修改某个COOKIE对象某个Key键的键值 或 给某个COOKIE对象添加Key键 都调用本方法
        ///


        /// 修改某个COOKIE对象某个Key键的键值 或 给某个COOKIE对象添加Key键 都调用本方法,操作成功返回字符串"success",如果对象本就不存在,则返回字符串"CookieNonexistence"。
        ///

        /// Cookie对象名称
        /// Key键名
        /// Key键值
        /// COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365)。注意:虽是修改功能,实则重建覆盖,所以时间也要重设,因为没办法获得旧的有效期
        /// 如果对象本就不存在,则返回字符串"CookieNonexistence",如果操作成功返回字符串"success"。
        public static string Edit(string strCookieName, string strKeyName, string KeyValue,int iExpires)
        {
            if (HttpContext.Current.Request.Cookies[strCookieName] == null)
            {
                return "CookieNonexistence";
            }
            else
            {
                HttpCookie bjCookie = HttpContext.Current.Request.Cookies[strCookieName];
                objCookie[strKeyName] = Utils.EncodeURIComponent(KeyValue.Trim());
                objCookie.Domain = N0.Config.CommonConfig.strDomain;
                if (iExpires > 0)
                {
                    if (iExpires == 1)
                    {
                        objCookie.Expires = DateTime.MaxValue;
                    }
                    else
                    {
                        objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
                    }
                }
                HttpContext.Current.Response.Cookies.Add(objCookie);
                return "success";
            }
        }

        /// 删除COOKIE对象
        ///


        /// 删除COOKIE对象
        ///

        /// Cookie对象名称
        public static void Del(string strCookieName)
        {
            HttpCookie bjCookie = new HttpCookie(strCookieName.Trim());
            objCookie.Domain = N0.Config.CommonConfig.strDomain;
            objCookie.Expires = DateTime.Now.AddYears(-5);
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }
        /// 删除某个COOKIE对象某个Key子键
        ///
        /// 删除某个COOKIE对象某个Key子键,操作成功返回字符串"success",如果对象本就不存在,则返回字符串"CookieNonexistence"
        ///

        /// Cookie对象名称
        /// Key键名
        /// COOKIE对象有效时间(秒数),1表示永久有效,0和负数都表示不设有效时间,大于等于2表示具体有效秒数,31536000秒=1年=(60*60*24*365)。注意:虽是修改功能,实则重建覆盖,所以时间也要重设,因为没办法获得旧的有效期
        /// 如果对象本就不存在,则返回字符串"CookieNonexistence",如果操作成功返回字符串"success"。
        public static string Del(string strCookieName, string strKeyName, int iExpires)
        {
            if (HttpContext.Current.Request.Cookies[strCookieName] == null)
            {
                return "CookieNonexistence";
            }
            else
            {
                HttpCookie bjCookie = HttpContext.Current.Request.Cookies[strCookieName];
                objCookie.Values.Remove(strKeyName);
                objCookie.Domain = N0.Config.CommonConfig.strDomain;
                if (iExpires > 0)
                {
                    if (iExpires == 1)
                    {
                        objCookie.Expires = DateTime.MaxValue;
                    }
                    else
                    {
                        objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
                    }
                }
                HttpContext.Current.Response.Cookies.Add(objCookie);
                return "success";
            }
        }
    }
    /// Session操作类
    ///
    /// Session操作类
    ///

    public static class SessionCustom
    {
        /// 添加Session,调动有效期默认为23分钟
        ///
        /// 添加Session,调动有效期默认为23分钟
        ///

        /// Session对象名称
        /// Session值
        public static void Add(string strSessionName, string strValue)
        {
            HttpContext.Current.Session[strSessionName] = strValue;
            HttpContext.Current.Session.Timeout = 23;
        }
        /// 添加Session
        ///
        /// 添加Session
        ///

        /// Session对象名称
        /// Session值
        /// 调动有效期(分钟)
        public static void Add(string strSessionName, string strValue, int iExpires)
        {
            HttpContext.Current.Session[strSessionName] = strValue;
            HttpContext.Current.Session.Timeout = iExpires;
        }

        /// 读取某个Session对象值
        ///


        /// 读取某个Session对象值
        ///

        /// Session对象名称
        /// Session对象值
        public static string Get(string strSessionName)
        {
            if (HttpContext.Current.Session[strSessionName] == null)
            {
                return null;
            }
            else
            {
                return HttpContext.Current.Session[strSessionName].ToString();
            }
        }

        /// 删除某个Session对象
        ///


        /// 删除某个Session对象
        ///

        /// Session对象名称
        public static void Del(string strSessionName)
        {
            HttpContext.Current.Session[strSessionName] = null;
        }
    }

    /// Cache操作类
    ///


    /// Cache操作类
    ///

    public static class CacheCustom
    {
        /// 简单创建/修改Cache,前提是这个值是字符串形式的
        ///
        /// 简单创建/修改Cache,前提是这个值是字符串形式的
        ///

        /// Cache名称
        /// Cache值
        /// 有效期,秒数(使用的是当前时间+秒数得到一个绝对到期值)
        /// 保留优先级,1最不会被清除,6最容易被内存管理清除(1:NotRemovable;2:High;3:AboveNormal;4:Normal;5:BelowNormal;6:Low)
        public static void Insert(string strCacheName, string strValue, int iExpires, int priority)
        {
            TimeSpan ts = new TimeSpan(0, 0, iExpires);
            CacheItemPriority cachePriority;
            switch (priority)
            {
                case 6:
                    cachePriority = CacheItemPriority.Low;
                    break;
                case 5:
                    cachePriority = CacheItemPriority.BelowNormal;
                    break;
                case 4:
                    cachePriority = CacheItemPriority.Normal;
                    break;
                case 3:
                    cachePriority = CacheItemPriority.AboveNormal;
                    break;
                case 2:
                    cachePriority = CacheItemPriority.High;
                    break;
                case 1:
                    cachePriority = CacheItemPriority.NotRemovable;
                    break;
                default:
                    cachePriority = CacheItemPriority.Default;
                    break;
            }
            HttpContext.Current.Cache.Insert(strCacheName, strValue, null, DateTime.Now.Add(ts), System.Web.Caching.Cache.NoSlidingExpiration, cachePriority, null);
        }

        /// 简单读书Cache对象的值,前提是这个值是字符串形式的
        ///


        /// 简单读书Cache对象的值,前提是这个值是字符串形式的
        ///

        /// Cache名称
        /// Cache字符串值
        public static string Get(string strCacheName)
        {
            return HttpContext.Current.Cache[strCacheName].ToString();
        }

        /// 删除Cache对象
        ///


        /// 删除Cache对象
        ///

        /// Cache名称
        public static void Del(string strCacheName)
        {
            HttpContext.Current.Cache.Remove(strCacheName);
        }
    }

来自: http://hi.baidu.com/yuyang0105
请使用浏览器的分享功能分享到微信等