今天来尝试一下dotnetcore5.0 的session redis的用法, 顺便看看你是否有阻塞,在我以前的文章 你的项目真的需要Session吗?redis保存session性能怎么样? 和 asp.net mvc Session RedisSessionStateProvider锁的实现 有提到asp.net mvc session 锁的问题【默认内存保存是不存在】,前几天 发现beego session redis 是没有锁的 ,大家可以参考beego Session redis存储以及是否阻塞 和 beego Session redis源码解读, 这次我打算用VScode来创建项目,VScode的搭建可以参考 使用Visual Studio Code开发.NET Core看这篇就够了???????
1.创建项目SessionDemo,把launchSettings.json 的https部分移除
运行界面 如下, 表示项目okay
2添加必要的包
dotnet add package Microsoft.AspNetCore.DataProtection.StackExchangeRedis dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis dotnet add package Microsoft.AspNetCore.Http //使用Session时有扩展方法 dotnet add package Microsoft.AspNetCore.Session
修改Startup.cs文件如下
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; }); //写controller 路由需要 } // 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?}"); }); } } }
创建一个HomeController.cs 如下:
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); } } }
修改Index.cshtml 如下:
@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>
运行项目, 首先访问http://localhost:5000/home/index 设置session, 然后在进入http://localhost:5000/ ,页面首先点击 setsession, 完全返回后 ,再次点击get session。
结论setsession同时发起V1和V2的请求,V1和V2获取的session 是先前index 设置的test,V2 把session设置为V2保存,由于V1等待时间长一些, 所以V1在V2后面设置session值V1, 后面V3 获取的session是V1。同时V1和V2的响应时间就说明V1和V2之间没有阻塞