首页 > 其他分享 >自定义session Provider随笔[由多个请求阻塞排队处理发现]

自定义session Provider随笔[由多个请求阻塞排队处理发现]

时间:2023-12-08 15:59:10浏览次数:31  
标签:timeout 自定义 session context Provider null sessionItems public out

引用:Session,有没有必要使用它?

using IDH.Common.BaseInfoCacheManagement;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.SessionState;

namespace IdhWebApplication.Extensions.SessionProvider
{
    /// <summary>
    /// 使用Redis实现SessionStateStoreProviderBase
    /// </summary>
    public class RedisSessionStateStore : SessionStateStoreProviderBase
    {
        /// <summary>
        /// 创建新的Session执行
        /// </summary>
        public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout)
        {
            return CreateLegitStoreData(context, null, null, timeout);
        }

        internal static SessionStateStoreData CreateLegitStoreData(HttpContext context, ISessionStateItemCollection sessionItems, HttpStaticObjectsCollection staticObjects, int timeout)
        {
            if (sessionItems == null)
                sessionItems = new SessionStateItemCollection();
            if (staticObjects == null && context != null)
                staticObjects = SessionStateUtility.GetSessionStaticObjects(context);
            return new SessionStateStoreData(sessionItems, staticObjects, timeout);
        }

        public override void CreateUninitializedItem(HttpContext context, string id, int timeout)
        {
            RedisSessionState state = new RedisSessionState(null, null, timeout);
            BaseInfoCache.redisSessionCache.SetItem(id, state.ToJson(), timeout);
        }

        /// <summary>
        /// 从redis获取session数据
        /// </summary>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <param name="exclusive"></param>
        /// <param name="locked"></param>
        /// <param name="lockAge"></param>
        /// <param name="lockId"></param>
        /// <param name="actionFlags"></param>
        /// <returns></returns>
        private SessionStateStoreData DoGet(HttpContext context, string id, bool exclusive, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actionFlags)
        {
            locked = false;
            lockId = null;
            lockAge = TimeSpan.Zero;
            actionFlags = SessionStateActions.None;
            RedisSessionState state = RedisSessionState.FromJson(BaseInfoCache.redisSessionCache.GetItem(id));
            if (state == null)
            {
                return null;
            }
            BaseInfoCache.redisSessionCache.SetItem(id, state.ToJson(), state._timeout);
            return CreateLegitStoreData(context, state._sessionItems, state._staticObjects, state._timeout);
        }

        /// <summary>
        /// 取值的时候执行
        /// </summary>
        public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actionFlags)
        {
            return this.DoGet(context, id, false, out locked, out lockAge, out lockId, out actionFlags);
        }

        public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actionFlags)
        {
            return this.DoGet(context, id, true, out locked, out lockAge, out lockId, out actionFlags);
        }

        /// <summary>
        /// 新增 修改 移除键值时执行
        /// </summary>
        /// <param name="item">item.Items为当前所有的键值对</param>
        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem)
        {
            ISessionStateItemCollection sessionItems = null;
            HttpStaticObjectsCollection staticObjects = null;

            if (item.Items.Count > 0)
                sessionItems = item.Items;
            if (!item.StaticObjects.NeverAccessed)
                staticObjects = item.StaticObjects;

            RedisSessionState state2 = new RedisSessionState(sessionItems, staticObjects, item.Timeout);

            BaseInfoCache.redisSessionCache.SetItem(id, state2.ToJson(), item.Timeout);
        }

        #region "未实现方法"

        public override void Dispose()
        {

        }

        public override void EndRequest(HttpContext context)
        {

        }

        public override void InitializeRequest(HttpContext context)
        {

        }

        public override void ReleaseItemExclusive(HttpContext context, string id, object lockId)
        {
        }

        public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item)
        {
            BaseInfoCache.redisSessionCache.RemoveItem(id);
        }

        public override void ResetItemTimeout(HttpContext context, string id)
        {

        }

        public override bool SetItemExpireCallback(SessionStateItemExpireCallback expireCallback)
        {
            return true;
        }

        #endregion

    }

    /// <summary>
    /// 存储在redis的对象
    /// </summary>
    public class SessionStateItem
    {
        /// <summary>
        /// session数据
        /// </summary>
        public Dictionary<string,object> Dict { get; set; }

        /// <summary>
        /// 超时时间(分钟)
        /// </summary>
        public int Timeout { get; set; }
    }

    /// <summary>
    /// 将session对象数据格式处理
    /// </summary>
    internal sealed class RedisSessionState
    {
        internal ISessionStateItemCollection _sessionItems;
        internal HttpStaticObjectsCollection _staticObjects;
        internal int _timeout;

        internal RedisSessionState(ISessionStateItemCollection sessionItems, HttpStaticObjectsCollection staticObjects, int timeout)
        {
            this.Copy(sessionItems, staticObjects, timeout);
        }

        internal void Copy(ISessionStateItemCollection sessionItems, HttpStaticObjectsCollection staticObjects, int timeout)
        {
            this._sessionItems = sessionItems;
            this._staticObjects = staticObjects;
            this._timeout = timeout;
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <returns></returns>
        public string ToJson()
        {
            if (_sessionItems == null || _sessionItems.Count == 0)
            {
                return null;
            }

           var dict = new Dictionary<string, object>();

            string key;
            NameObjectCollectionBase.KeysCollection keys = _sessionItems.Keys;
            for (int i = 0; i < keys.Count; i++)
            {
                key = keys[i];
                dict.Add(key, _sessionItems[key]);
            }

            var item = new SessionStateItem { Dict = dict, Timeout = this._timeout };

            return JsonConvert.SerializeObject(item);
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static RedisSessionState FromJson(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return null;
            }
            try
            {
                SessionStateItem item = JsonConvert.DeserializeObject<SessionStateItem>(json);

                SessionStateItemCollection collections = new SessionStateItemCollection();

                foreach (var kvp in item.Dict)
                {
                    //转换指定类型,要不然从session获取的对象无法强转为指定类型
                    if(kvp.Key == "CurrentUserPermissions")
                    {
                        var permissionJson = JsonConvert.SerializeObject(kvp.Value);
                        collections[kvp.Key] = JsonConvert.DeserializeObject<Dictionary<Guid,int>>(permissionJson);
                    }
                    else if(kvp.Key == "CurrentUser")
                    {
                        var userJson = JsonConvert.SerializeObject(kvp.Value);
                        collections[kvp.Key] = JsonConvert.DeserializeObject<UserIdentity>(userJson);
                    }
                    else
                    {
                        collections[kvp.Key] = kvp.Value;
                    }
                }

                return new RedisSessionState(collections, null, item.Timeout);
            }
            catch
            {
                return null;
            }
        }
    }

}

  

再配置文件system.web节点下添加代码

<sessionState mode="Custom" customProvider="RedisSessionStateStore">
		  <providers>
			  <add name="RedisSessionStateStore" type="IdhWebApplication.Extensions.SessionProvider.RedisSessionStateStore"/>
		  </providers>
	  </sessionState>

  

标签:timeout,自定义,session,context,Provider,null,sessionItems,public,out
From: https://www.cnblogs.com/caihuaxing/p/17888300.html

相关文章

  • scrapy框架之自定义扩展
    自定义扩展时,利用信号在指定位置注册制定操作源码剖析:1fromscrapy.extensions.telnetimportTelnetConsole#查看TelnetConsole源码23#Enableordisableextensions4#Seehttps://doc.scrapy.org/en/latest/topics/extensions.html5EXTENSIONS={6'scrapy.......
  • scrapy框架之自定义简易scrapy框架
    自定义low版Scrapy框架:1fromtwisted.internetimportreactor#事件循环(终止条件,所有的socket都已经移除)2fromtwisted.web.clientimportgetPage#socket对象(如果下载完成..自动从事件循环中移除)3fromtwisted.internetimportdefer#defer.Deferred特殊的soc......
  • 12、QT自定义消息框
     #include"dialog.h"Dialog::Dialog(QWidget*parent):QDialog(parent){resize(260,80);glayout=newQGridLayout(this);labelmsg=newQLabel("自定义消息框");msgButton=newQPushButton("测试操作");labeldis......
  • zabbix使用自定义监控项权限不足
    一、背景zabbix在配置好个人监控项后,最开始基本上都会遇到权限不足的问题。二、解决方法两种解决方法:1、配置sudoers,然后再key值执行命令前加sudo(相对来说比较麻烦)2、配置zabbix_agent#zabbix-agent配置echo"AllowRoot=1">>/etc/zabbix/zabbix_agentd.conffind/-na......
  • Mybatis Plus 自定义 TypeHandler
    在MyBatisPlus中,可以自定义TypeHandler来处理特殊的类型转换。下面是如何自定义一个TypeHandler的步骤:我们需要创建一个实现org.apache.ibatis.type.TypeHandler接口的类。这个类需要实现以下几个方法:setParameter(PreparedStatementps,inti,Tparameter,JdbcTypejdbc......
  • SpringBoot高级开发(9)Spring中的HttpSession
    1、简述HttpSession是javaWeb提供的,用来处理会话事务的。session数据保存在后台,当然首次开启会话(即调用req.getSession())的时候也会将该SessionID数值传给前端用作Cookie2、作用范围首次访问服务器开始,浏览器关闭后就结束。后端的Session可以存储30分钟,如果30分钟无任何请求,就......
  • 自定义idea工具栏图标
    把我们常用的一些图标放在工具栏,随用随点可以提升工作效率,以下是自定义idea工具栏图标的步骤进入工具栏菜单设置页面添加图标:如果我们想添加一个push按钮,就搜索push,点击确定按钮添加push调节按钮到合适位置,点击OK完成添加按钮已经添加完毕......
  • 关键字 开发-10 封装引用自定义函数变量
    前言前面在yaml文件中引用内置函数以及自定义函数和变量时,都是在每个关键字后面进行单独得渲染,为了方便引用,于是我们单独对这块的内容进行封装。1.新增自定义函数和变量在utils下新建自定义函数和变量的文件,my_builtins.py,新增了在接口中需要用到的一些变量和函数。这样,在传......
  • antd Pro组件ProFormList实现自定义action
    antdPro组件ProFormList实现自定义actionProFormList是antdesignpro的结构化数据组件,通常用来实现动态表单。现在有个需求,除了组件自带的删除和复制,还需要增加两个按钮来实现每个item位置的上下移动,如图所示:查看官方文档,组件有提供自定义action的API--actionRender,介绍如下......
  • java 自定义查询StringBuffer Sql
    一、背景二、实现@AutowiredprivateEntityManagerentityManager;Queryquery=entityManager.createNativeQuery(sql);query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);List<Map<String,Object>>reportWaterTota=qu......