国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#語法糖之Cookies操作類 asp.net

2019-11-17 02:09:26
字體:
來源:轉載
供稿:網友

C#語法糖之Cookies操作類 asp.net

用法:

   //聲名一個數據集合            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(key);            cookiesManager.Remove(key);//刪除            cookiesManager.RemoveAll(c => c.Contains("sales_"));//刪除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 (默認1天)        /// </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(V) == typeof(string))                    {                        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(value);                        Add(key, cookiesDurationInSeconds, cookie, setValue, response);                    }                }            }        }        private void Add(string key, int cookiesDurationInSeconds, HttpCookie cookie, string setValue, HttpResponse response)        {            if (!string.IsNullOrEmpty(key) && cookie.HasKeys)                cookie.Values.Set(key, setValue);            else                if (!string.IsNullOrEmpty(setValue))                    cookie.Value = setValue;            if (cookiesDurationInSeconds > 0)                cookie.Expires = DateTime.Now.AddSeconds(cookiesDurationInSeconds);            response.SetCookie(cookie);        }        public override bool ContainsKey(string key)        {            return Get(key) != 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(V) == typeof(string))            {                return (V)Convert.ChangeType(value, typeof(V));            }            else            {                System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();                return jss.Deserialize<V>(value);            }        }        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(-1);                if (cookie != null)                {                    if (!string.IsNullOrEmpty(key) && cookie.HasKeys)                        cookie.Values.Remove(key);                    else                        request.Cookies.Remove(key);                }            }        }        public override void RemoveAll()        {            foreach (var key in GetAllKey())            {                Remove(key);            }        }        public override void RemoveAll(Func<string, bool> removeExpression)        {            var removeList = GetAllKey().Where(removeExpression).ToList();            foreach (var key in removeList)            {                Remove(key);            }        }        public override V this[string key]        {            get { return Get(key); }        }    }}

  

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

  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 琼结县| 蕉岭县| 连平县| 凌云县| 自贡市| 汉沽区| 榆中县| 景宁| 吉木乃县| 施甸县| 石楼县| 江源县| 天津市| 昌黎县| 台中县| 华坪县| 宁都县| 镶黄旗| 威信县| 公主岭市| 凌海市| 隆林| 红桥区| 郧西县| 湘乡市| 屏东市| 逊克县| 神农架林区| 佛坪县| 桓仁| 玉门市| 伊吾县| 开原市| 乌鲁木齐县| 东明县| 塘沽区| 台前县| 邛崃市| 龙胜| 甘肃省| 中江县|