session操作类

Posted 记性特差

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了session操作类相关的知识,希望对你有一定的参考价值。

实现代码:

//声名一个数据集合
         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; }
    }
}

 

以上是关于session操作类的主要内容,如果未能解决你的问题,请参考以下文章

在“设置”片段中夸大类PreferenceScreen的错误

在Android中,如何将数据从类传递到相应的布局/片段文件?

如何通过单击片段内的线性布局从片段类开始新活动?下面是我的代码,但这不起作用

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程