ASP.NET Core 由于其更简洁、更轻便的架构和跨平台支持,在开发 Web 应用程序方面正变得越来越流行。 这样的ASP.NET Core 应用程序是高流量的,并且在负载平衡的多服务器部署中运行。 事实上,通常会看到 10-20 个服务器网络场,甚至比这还要大。
拥有多服务器负载平衡部署使您的应用程序层非常可扩展,因为您可以随着事务负载的增加添加更多服务器。 这允许您的 ASP.NET Core 应用程序轻松处理繁重的事务负载。 仍然存在性能瓶颈,这会减慢您的 ASP.NET Core 应用程序。
而这个 ASP.NET Core 性能瓶颈在于您的数据库和数据存储,它们无法像您的 ASP 那样处理繁重的负载.NET Core 应用层可以。 虽然您可以向应用层网络场添加更多服务器,但您不能对数据库层做同样的事情。 下面是成为ASP性能瓶颈的两种数据存储方式.NET Core 领域广泛应用,提供了卓越的解决方案。
- 数据库服务器 (SQL Server)
- ASP.NET Core 会议
NCache 更多信息 NCache 配套文档 NCache 客户端API
解决方案:分布式缓存
删除这些数据存储 性能瓶颈,你最好的选择是使用分布式缓存,如 NCache. NCache 是一个 .NET 开源内存分布式缓存,比数据库快得多。 与您的数据库不同, NCache is 线性可扩展 因为它允许您构建缓存服务器集群并随着事务负载的增加向集群中添加更多服务器。
NCache 允许您缓存应用程序数据,这样您就可以将那些昂贵的数据库访问次数减少近 80%。 这减少了数据库的负载,使其能够更快地执行读取和写入操作,而不再成为性能瓶颈。
NCache 也是您的可扩展分布式存储 ASP.NET Core 招生面试。 此外, NCache 复制 ASP.NET Core 与多个服务器的会话,以防止在任何缓存服务器出现故障时丢失数据。 ASP.NET Core 会话,这非常重要,因为您不能在运行时丢失任何会话。 下图显示了分布式缓存如何 NCache 适合您的应用程序部署。
NCache 更多信息 NCache 配套文档 NCache 客户端API
通过 ASP 缓存应用数据.NET Core ID分布式缓存
ASP之前.NET Core,较旧的 ASP.NET 提供了一个独立的 ASP.NET 缓存 不能满足多服务器环境的需求。 现在,ASP.NET Core 已经介绍了 ID分布式缓存 接口作为一个相当基本的分布式缓存标准 API,允许您对其进行编程,然后无缝地插入第三方分布式缓存。
这是一个如何使用的示例 IDistributedCache
接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
IDistributedCache _cache; ... private byte[] LoadCustomer(string custId) { string key = "Customers:CustomerID:" + custId; // is the customer in the cache? byte[] customer = _cache.Get(key); if (customer == null) { // the cache doesn't have it. so load from DB customer = LoadFromDB(key); // And, cache it for next time _cache.Set(key, customer); } return customer; } |
NCache 还实施了一个提供者 IDistributedCache
你可以插入到你的 ASP.NET Core 应用程序。 这样,您不必更改任何特定于 NCache.
这是什么 IDistributedCache
接口看起来像(请注意,这些方法中的每一个也有一个异步重载)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
namespace Microsoft.Extensions.Caching.Distributed { public interface IDistributedCache { // Each of these methods also has an “Async” overload byte[] Get(string key); void Refresh(string key); void Remove(string key); // Specify absolute & sliding expiration through options void Set(string key, byte[] value, DistributedCacheEntryOptions options); } } |
配置 NCache 作为 IDistributedCache 提供者
这是您的配置方式 NCache 身为你的 IDistributedCache
ASP 中的提供程序.NET Core Startup
类。
1 2 3 4 5 6 7 8 9 10 11 |
public class Startup { ... public void ConfigureServices (IServiceCollection services) { ... services.AddNCacheDistributedCache(); ... } ... } |
NCache 更多信息 ASP.NET Core 高速缓存 ASP.NET Core 会话提供者
为什么选择 NCache 基于 IDistributedCache 的 API?
如果您的缓存需求相当基本,并且您希望灵活地无缝更改分布式缓存供应商,那么您应该继续使用 ID分布式缓存 界面。 它允许您无缝地更改缓存供应商。 但是要权衡没有很多高级缓存功能的成本。
另一个选择是使用 NCache API 直接来自您的 ASP.NET Core 应用程序。 NCache API 与遗留的 ASP.NET Cache API 非常相似。 它包含大量免费功能,使您能够充分利用企业级分布式缓存。
请记住,您可以缓存的数据越多,应用程序的性能和可扩展性优势就越大。 而且,如果没有高级缓存功能,您通常只能缓存只读或简单数据。 阅读更多关于所有不同的信息 NCache 缓存功能 你错过了使用 IDistributedCache
供应商。
存储 ASP.NET Core 分布式缓存中的会话
在 ASP 之前.NET Core,较旧的 ASP.NET 提供了一个 ASP.NET 会话状态提供程序框架,允许第三方会话存储提供程序插入。 ASP.NET Core session 为插入第三方存储提供程序提供了类似的机制。 下面是两种使用方法 NCache 作为ASP.NET Core 会话存储:
使用 NCache ASP.NET Core 通过 IDistributedCache 的会话
只要你配置 NCache as IDistributedCache
ASP 提供者.NET Core, NCache 自动成为 ASP 的默认存储选项.NET Core 会话,您无需执行任何其他操作。 但请注意,与旧版本(在 ASP.NET Core) ASP.NET 会话状态。
下面是一些默认 ASP 的东西.NET Core 会话实现缺乏:
- 会话锁定: ASP.NET Core 不提供会话锁定。 这是更早的 ASP.NET 会话状态提供的东西。
byte[]
自定义对象的数组: ASP.NET Core 强制您将所有自定义对象转换为字节数组,然后才能存储在会话中。 甚至较旧的 ASP.NET 会话状态也支持自定义对象。
使用 NCache 作为ASP.NET Core 会话提供者
解决默认 ASP.NET Core 会话实施通过 IDistributedCache
供应商, NCache 已经实施了自己的 ASP.NET Core 会话提供者。 此实现具有比默认实现更多的功能。
这是您在您的 Startup
类。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Startup { ... public void Configure(IApplicationBuilder app, IHostingEnvironment env) { ... app.UseNCacheSession(); ... } ... } |
您可以指定 ASP.NET Core 上面的会话配置在 appsettings.json
文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ ... "NCacheSessions": { ... "CacheName": "demoCache", "EnableLogs": "True", "RequestTimeout": "90", "EnableDetailLogs": "False", "ExceptionsEnabled": "True", "WriteExceptionsToEventLog": "False" } ... } |
NCache 更多信息 NCache 配套文档 配置 NCache IDistributedCache 提供程序
结论
微软提供了两种选择 IDistributedCache
提供者。 一个是 SQL Server,第二个是 Redis. NCache 比这两种选择都好。 与 SQL Server 相比, NCache 更快,更具可扩展性。 还, NCache 也是一个比 Redis 出于以下原因:
- 本机 .NET: NCache 是 100% 原生 .NET,因此非常适合您的 .NET 应用程序堆栈。 另一方面, Redis 来自 Linux 背景,不是原生 .NET 缓存。
- 比...快 Redis: NCache 实际上比...更快 Redis as NCache 客户端缓存功能提供 NCache 显着的性能提升。
- 更多功能: NCache 提供了许多非常重要的分布式缓存功能, Redis 才不是。 在此查看更多详细信息 Redis vs NCache.