C#文法糖之 session操作類 asp.net

來源:互聯網
上載者:User

標籤:

用法:

   //聲名一個資料集合            var listString = new List<string>() { "a", "b", "c" };            //session key            string key = "sekey";            //擷取執行個體            var sessionManager = SessionManager<List<string>>.GetInstance();            //添加session            sessionManager.Add(key, listString);            //add有其它重載 上面是最基本的            //擷取            List<string> sessionList = sessionManager[key];            //其它方法            sessionManager.ContainsKey(key);            sessionManager.Remove(key);//刪除            sessionManager.RemoveAll(c => c.Contains("sales_"));//刪除key包含sales_的session            sessionManager.GetAllKey();//擷取所有key

  

 

 

代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;namespace SyntacticSugar{    /// <summary>    /// ** 描述:session操作類    /// ** 創始時間:2015-6-9    /// ** 修改時間:-    /// ** sunkaixuan    /// ** 使用說明:    /// </summary>    /// <typeparam name="K">鍵</typeparam>    /// <typeparam name="V">值</typeparam>    public class SessionManager<V> : IHttpStorageObject<V>    {        private static readonly object _instancelock = new object();        private static SessionManager<V> _instance = null;        public static SessionManager<V> GetInstance()        {            if (_instance == null)            {                lock (_instancelock)                {                    if (_instance == null)                    {                        _instance = new SessionManager<V>();                    }                }            }            return _instance;        }        public override void Add(string key, V value)        {            context.Session.Add(key, value);        }        public override bool ContainsKey(string key)        {            return context.Session[key] != null;        }        public override V Get(string key)        {            return (V)context.Session[key];        }        public override IEnumerable<string> GetAllKey()        {            foreach (var key in context.Session.Keys)            {                yield return key.ToString();            }        }        public override void Remove(string key)        {            context.Session[key] = null;            context.Session.Remove(key);        }        public override void RemoveAll()        {            foreach (var key in GetAllKey())            {                Remove(key);            }        }        public override void RemoveAll(Func<string, bool> removeExpression)        {            var allKeyList = GetAllKey().ToList();            var removeKeyList = allKeyList.Where(removeExpression).ToList();            foreach (var key in removeKeyList)            {                Remove(key);            }        }        public override V this[string key]        {            get { return (V)context.Session[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; }    }}

  

C#文法糖之 session操作類 asp.net

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.