编程知识 cdmana.com

Is there any blocking in using dotnetcore 5.0 session redis

Try it today dotnetcore5.0 Of session redis Usage of , By the way, see if you have a blockage , In my previous article   Your project really needs Session Do you ?redis preservation session How about the performance ?  and  asp.net mvc Session RedisSessionStateProvider Lock implementation   Have mentioned asp.net mvc session The lock problem 【 The default memory save does not exist 】, the other day Find out beego session redis There is no lock , You can refer to beego Session redis Storage and blocking and  beego Session redis Source code interpretation , This time I'm going to use VScode To create the project ,VScode You can refer to   Use Visual Studio Code Development .NET Core Just read this one ​​​​​​​

1. Create project SessionDemo, hold launchSettings.json Of https Partially removed

Operation interface as follows , Represents a project okay

 

2 Add the necessary packages

dotnet add package Microsoft.AspNetCore.DataProtection.StackExchangeRedis
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
dotnet add package Microsoft.AspNetCore.Http  // Use Session There are extension methods 
dotnet add package Microsoft.AspNetCore.Session 

modify Startup.cs The documents are as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.DataProtection;
using StackExchange.Redis;
namespace SessionDemo
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            // add by gavin for session redis
            var redisConn = "127.0.0.1:6379";
                   
            services.AddDataProtection()
                .SetApplicationName("vextus")
                .PersistKeysToStackExchangeRedis(ConnectionMultiplexer.Connect(redisConn),"DataProtection-Keys");

            services.AddStackExchangeRedisCache(o =>
            {
                o.Configuration = redisConn;
            });
            services.AddSession();
            services.AddMvc(x=> { x.EnableEndpointRouting = false; }); // Write controller  Routing needs 
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
             // add by gavin for session redis
            app.UseSession();
            app.UseMvc(x => { x.MapRoute( name: "default",template: "{controller=Home}/{action=Index}/{id?}"); });
        }
    }
}
View Code

Create a HomeController.cs as follows :

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SessionDemo
{
     public class HomeController : Controller
    {
        public IActionResult Index()
        {
            this.HttpContext.Session.SetString("name", "test");
            return Content("Home");
        }
        public IActionResult V1()
        {
            var txt = this.HttpContext.Session.GetString("name");
            Thread.Sleep(1000 * 4);
            this.HttpContext.Session.SetString("name", "V1");
            return Content("Get Session:"+txt+" set session:V1");
        }
        public IActionResult V2()
        {
            var txt = this.HttpContext.Session.GetString("name");
            Thread.Sleep(1000 * 1);
            this.HttpContext.Session.SetString("name", "V2");
            return Content("Get Session:" + txt + " set session:V2");
        }
        public IActionResult V3()
        {
            var txt = this.HttpContext.Session.GetString("name");

            return Content("Get Session:" + txt);
        }
    }
}
View Code

modify Index.cshtml as follows :

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
<div>test</div>
        <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
        <input type="button" value="set session" id="setsession" /><br />
        V1: <p id="txt1"></p><br />
        V2:  <p id="txt2"></p><br />
        <input type="button" value="get session" id="getsession" /><br />
        V3:  <p id="txt3"></p><br />
        <script>
        $("#setsession").click(function(){
            $.get("http://localhost:5000/home/v1",function(data){$("#txt1").html(data) ;});
            $.get("http://localhost:5000/home/v2",function(data){$("#txt2").html(data) ;});

        });
        $("#getsession").click(function(){
            $.get("http://localhost:5000/home/v3",function(data){$("#txt3").html(data) ;});

        });
        </script>
</div>
View Code

Run the project , First visit http://localhost:5000/home/index Set up session, And then into http://localhost:5000/ , The page first clicks on setsession, After returning completely , Click again on the get session.

 

  Conclusion setsession At the same time initiate V1 and V2 Request ,V1 and V2 Acquired session It was before index Set up test,V2 hold session Set to V2 preservation , because V1 Wait a little longer , therefore V1 stay V2 Set it later session value V1, Back V3 Acquired session yes V1. meanwhile V1 and V2 The response time of the V1 and V2 There is no obstruction between

 

版权声明
本文为[dz45693]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224091951913t.html

Scroll to Top