语法糖之Cookies操作类

作者: 编程应用  发布:2019-09-26

用法:

   //声名一个数据集合            var listString = new List<string>() { "a", "b", "c" };            //缓存key            string key = "cokey";            //获取实例            var cookiesManager = CookiesManager<List<string>>.GetInstance();            //插入缓存            cookiesManager.Add(key, listString, cookiesManager.Minutes * 30);//过期30分钟             //add有其它重载 上面是最基本的            //获取            List<string> cookiesList = cookiesManager[key];            //其它方法            cookiesManager.ContainsKey;            cookiesManager.Remove;//删除            cookiesManager.RemoveAll(c => c.Contains);//删除key包含sales_的cookies            cookiesManager.GetAllKey();//获取所有key

  

代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;namespace SyntacticSugar{    /// <summary>    /// ** 描述:cookies操作类    /// ** 创始时间:2015-6-9    /// ** 修改时间:-    /// ** 作者:sunkaixuan    /// ** 使用说明:    /// </summary>    /// <typeparam name="V">值</typeparam>    public class CookiesManager<V> : IHttpStorageObject<V>    {        #region 全局变量        private static CookiesManager<V> _instance = null;        private static readonly object _instanceLock = new object();        #endregion        /// <summary>                 /// 获取实例                /// </summary>                 /// <returns></returns>                 public static CookiesManager<V> GetInstance()        {            if (_instance == null)                lock (_instanceLock)                    if (_instance == null)                        _instance = new CookiesManager<V>();            return _instance;        }        /// <summary>        /// 添加cookies ,注意value最大4K         /// </summary>        /// <param name="key">key</param>        /// <param name="value">value</param>        public override void Add(string key, V value)        {            Add(key, value, Day);        }        /// <summary>        /// 添加cookies ,注意value最大4K        /// </summary>        /// <param name="key"></param>        /// <param name="value"></param>        /// <param name="cookiesDurationInSeconds">有效时间单位秒</param>        public void Add(string key, V value, int cookiesDurationInSeconds)        {            HttpResponse response = HttpContext.Current.Response;            if (response != null)            {                HttpCookie cookie = response.Cookies[key];                if (cookie != null)                {                    if  == typeof                    {                        string setValue = value.ToString();                        Add(key, cookiesDurationInSeconds, cookie, setValue, response);                    }                    else                    {                        System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();                        string setValue = jss.Serialize;                        Add(key, cookiesDurationInSeconds, cookie, setValue, response);                    }                }            }        }        private void Add(string key, int cookiesDurationInSeconds, HttpCookie cookie, string setValue, HttpResponse response)        {            if (!string.IsNullOrEmpty && cookie.HasKeys)                cookie.Values.Set(key, setValue);            else                if (!string.IsNullOrEmpty)                    cookie.Value = setValue;            if (cookiesDurationInSeconds > 0)                cookie.Expires = DateTime.Now.AddSeconds(cookiesDurationInSeconds);            response.SetCookie;        }        public override bool ContainsKey(string key)        {            return Get != null;        }        public override V Get(string key)        {            string value = string.Empty;            if (context.Request.Cookies[key] != null)                value = context.Request.Cookies[key].Value;            if  == typeof            {                return Convert.ChangeType(value, typeof;            }            else            {                System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();                return jss.Deserialize<V>;            }        }        public override IEnumerable<string> GetAllKey()        {            var allKeyList = context.Request.Cookies.AllKeys.ToList();            foreach (var key in allKeyList)            {                yield return key;            }        }        public override void Remove(string key)        {            HttpRequest request = HttpContext.Current.Request;            if (request != null)            {                HttpCookie cookie = request.Cookies[key];                cookie.Expires = DateTime.Now.AddDays;                if (cookie != null)                {                    if (!string.IsNullOrEmpty && cookie.HasKeys)                        cookie.Values.Remove;                    else                        request.Cookies.Remove;                }            }        }        public override void RemoveAll()        {            foreach (var key in GetAllKey            {                Remove;            }        }        public override void RemoveAll(Func<string, bool> removeExpression)        {            var removeList = GetAllKey().Where(removeExpression).ToList();            foreach (var key in removeList)            {                Remove;            }        }        public override V this[string key]        {            get { return Get; }        }    }}

  

using System;namespace SyntacticSugar{    public abstract class IHttpStorageObject<V>    {        public int Minutes = 60;        public int Hour = 60 * 60;        public int Day = 60 * 60 * 24;        public System.Web.HttpContext context = System.Web.HttpContext.Current;        public abstract void Add(string key, V value);        public abstract bool ContainsKey(string key);        public abstract V Get(string key);        public abstract global::System.Collections.Generic.IEnumerable<string> GetAllKey();        public abstract void Remove(string key);        public abstract void RemoveAll();        public abstract void RemoveAll(Func<string, bool> removeExpression);        public abstract V this[string key] { get; }    }}

  

本文由金沙澳门官网送注册58发布于编程应用,转载请注明出处:语法糖之Cookies操作类

关键词:

上一篇:没有了
下一篇:没有了