NCache 看似与 ASP 集成.NET Core 框架。 和 NCache,我们可以在我们的 ASP 中缓存应用程序数据或响应.NET Core 应用程序。 我们再看看另一个 NCache ASP 的功能.NET Core 应用程序:使用 ASP 进行会话状态缓存.NET Core.
HTTP 和会话状态
首先介绍一下背景。 HTTP 是一种无状态协议。 这意味着请求是相互独立的,并且服务器不保存以前请求的用户数据。 但是,如果我们需要在用户浏览我们的 Web 应用程序时存储用户数据,我们需要以某种方式在请求之间保留用户值。
ASP.NET Core 官方文档描述了几种方法 国家管理. 如 cookie、隐藏字段和查询字符串等。
要在请求之间保留用户数据,ASP.NET Core 依赖于 cookie 和服务器端存储机制。 对于每个请求,ASP.NET Core 传递带有标识符的 cookie 以在服务器端获取会话数据。 默认情况下,ASP.NET Core 使用内存存储会话数据。
但是使用内存存储会话数据有一些缺点。 万一我们的应用程序服务器脱机,我们将丢失会话数据。 如果我们重新路由我们的流量,我们不会在新位置复制会话数据。 而且,如果我们有一个服务器场,我们需要将会话绑定到特定的应用程序实例,从而使我们的应用程序难以扩展。
NCache 用于 HTTP 会话状态缓存
我们可以使用 NCache 用于会话状态缓存。
NCache,准确地说,我们将会话数据存储在集群中的不同进程中。 这样,我们就不会受到应用程序服务器内存量的限制。 我们可以通过向其添加更多服务器来扩展我们的缓存集群,从而增加我们的事务和存储容量。
由于 NCache 缓存拓扑 和会话复制,如果我们的应用程序服务器离线,我们仍然可以在所有服务器节点上使用我们的会话数据。
NCache,我们可以跨其他区域或数据中心复制我们的会话数据。 例如,在灾难恢复的情况下,我们总是有可用的会话数据。 甚至我们可以将会话数据保留在其创建位置,并且仅当我们在区域之间移动流量时才移动会话数据,从而保持较低的带宽消耗成本。
使用的更多好处 NCache 对于会话状态,检查 ASP.NET Core 缓存优势和概述.
我们只需要更改一些代码即可使用 NCache 用于会话状态缓存。 我们有两种方法:使用 NCache 作为会话提供者或 IDistributedCache
供应商。
在使用之前 NCache 作为会话提供者,我们需要 AspNetCore.Session.NCache
NuGet 包并添加 Alachisoft.NCache.Web.SessionState
命名空间。
让我们添加 NCache 作为示例 .NET 6.0 ASP 的会话提供程序.NET Core Web应用程序。 像这样,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using Alachisoft.NCache.Web.SessionState; var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddControllersWithViews(); // 1. Add NCache as a Session Provider builder.Services.AddNCacheSession(config => { config.CacheName = "demoClusteredCache"; // To log all error information. (Optional) config.EnableLogs = true; // An identifier to make SessionId unique between applications. (Optional) config.SessionAppId = "demoApp"; }); // Alternatively, read settings from the appsettings.json file // //var ncacheSettingsSection = builder.Configuration.GetSection("NCacheSettings"); //builder.Services.AddNCacheSession(ncacheSettingsSection); var app = builder.Build(); if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); // 2. Store NCache session data app.UseNCacheSession(); app.MapRazorPages(); app.MapDefaultControllerRoute(); app.Run(); |
首先,我们添加 NCache 将会话状态缓存到 ASP 的服务.NET Core 依赖容器。 为此,我们使用 AddNCacheSession()
方法。 它期望一个 NCacheSessionConfiguration
目的。 我们需要将缓存名称作为必需参数传递。 我们可以选择启用日志(EnableLogs = true
) 并传递一个应用程序标识符,使会话 ID 在使用相同会话数据的应用程序之间是唯一的 (SessionAppId = "demoApp"
).
我们可以使用 appsettings.json
文件并将配置部分传递给 AddNCacheSession()
方法。
这是 appsettings.json
上一个示例中的设置文件,
1 2 3 4 5 6 7 8 |
{ "NCacheSettings": { "CacheName": "demoClusteredCache", "EnableLogs": "true", "SessionAppId": "demoApp" }, // Other settings here... } |
有关设置的更多详细信息 NCache 有它的会话提供者,检查 ASP.NET Core 会话提供者配置.
一旦我们注册 NCache 服务,我们需要配置HTTP请求管道。 我们需要添加 UseNCacheSession()
方法。 让我们关注请求管道中中间件的顺序。 我们应该添加 NCache 会话中间件在任何其他读取会话数据的中间件之前。
此外,添加后 NCache 服务和中间件,我们使用 Http.Session
Razor 页面和 MVC 控制器中的类。
2. NCache 会话的 IDistributedCache 实现
而不是使用 AddNCacheSession()
和 UseNCacheSession()
方法 注册 NCache 作为 Session Provider,我们可以使用 ASP.NET Core 与会议 NCache IDistributedCache 实现.
默认情况下,ASP.NET Core 有 AddDistributedMemoryCache()
注册内存中实现的方法 IDistributedCache
.
我们不使用该实现,而是添加 NCache 使用 AddNCacheDistributedCache()
方法。
这次我们安装 NCache.Microsoft.Extensions.Caching
NuGet 打包并使用 Alachisoft.NCache.Caching.Distributed
命名空间。 让我们将之前的示例 Web 应用程序更改为使用 NCache IDistributedCache
实施,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using Alachisoft.NCache.Caching.Distributed; var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddControllersWithViews(); // 1. Add NCache Distributed Cache implementation builder.Services.AddNCacheDistributedCache(config => { config.CacheName = "demoClusteredCache"; // To log all error information. (Optional) config.EnableLogs = true; }); // 2. Add ASP.NET Core Session services builder.Services.AddSession(); var app = builder.Build(); if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); // 3. Add ASP.NET Core Session middleware app.UseSession(); app.MapRazorPages(); app.MapDefaultControllerRoute(); app.Run(); |
注意,而不是注册 NCache 会话状态缓存的服务和中间件,我们使用ASP.NET Core AddSession()
和 UseSession()
方法。 但是,我们指定了一个不同的分布式缓存实现。 那个来自 NCache.
要使用 AddNCacheDistributedCache()
方法,我们需要将缓存名称作为必需参数传递。 同样,我们可以使用 appsetttings.json
文件代替。 或者,我们可以启用日志和异常以及更改请求超时和重试尝试。 对于所有可用的配置选项 IDistributedCache
实施、检查 配置 ASP.NET Core ID分布式缓存.
完成这些步骤后,我们可以使用 Http.Session
.
结论
这就是我们如何扩展我们的 ASP.NET Core 使用会话状态缓存的应用程序 NCache. 因此,只需在我们的应用程序中进行一些代码更改和配置。
NCache 有更多的状态管理功能。 例如, NCache 像一个 多区域 ASP.NET Core 会话提供者. 此外,在灾难恢复或流量重新路由的情况下,我们可以复制我们的会话数据以避免丢失我们的会话数据。
此外,对于那些仍然使用 ASP 的遗留代码库的应用程序.NET Framework 与新的 ASP 一起.NET Core, 我们可以 配置 NCache 在两个框架之间共享会话.
所以,仔细看看 NCache 通过检查会话缓存 NCache GitHub 官方回购 对于使用会话缓存构建猜谜游戏的示例应用程序。