得益于纯净、轻量化而且跨平台支持的特性,ASP.NET Core做为热门Web应用开发框架,其高性能传输和负载均衡的支持已广受青睐。实际上,10-20台Web服务器仍是轻松驾驭的。有了多服务器负载的支持,使得Web应用层在业务增加时随时采用水平扩展,ASP.NET Core也可以没有什么负担地处理长事务。然而形成性能瓶颈的地方仍然不可忽视,具体来讲首当其冲就是数据存储,没法随着应用层的性能提高而提升大规模数据处理能力,这是由于数据层是没有办法简单经过增长服务器获得改善的。php
ASP.NET Core应用有两类数据在数据存储成为瓶颈时突显出来:html
解决这个性能瓶颈,不妨试试NCache,它是一个开源的支持.NET的分布式缓存,它的优点在于彻底基于内存,因此你能够在业务增加时组建内存服务器的集群来实现线性扩展,相比于数据库能节省近八成的成本,而且在读写应用数据上获得更快的体验。NCache很适合存储session会话,在多Web服务器负载时也解决了会话保持的需求。redis
下图是NCache这类常见的分布式缓存的部署架构。数据库
IDistributedCache
在ASP.NET Core以前,旧的ASP.NET程序的缓存对象是独立进程的,也没有多服务器支持的须要。现今ASP.NET Core推出了IDistributedCache
统一接口,定义了分布式缓存的基本接口,相似于日志、注入容器的接口同样,能够无缝提供第三方扩展。json
IDistributedCache
接口使用示例:数组
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 Cor应用中使用NCache不须要特殊的配置。缓存
如下是IDistributedCache
接口的定义(注意每一个方法都有对应的Async版本的重载)。服务器
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 thru options void Set(string key, byte[] value, DistributedCacheEntryOptions options); } }
接下来在ASP.NET Core的Startup
类中配置NCacheIDistributedCache
session
public class Startup { ... public void ConfigureServices(IServiceCollection services) { ... services.AddNCacheDistributedCache(); ... } ... }
若是对缓存需求不高, 使用IDistributedCache
保留了更改分布式缓存灵活性。可是要权衡这种放弃高级缓存特性的成本。为何不考虑一下NCache?NCache API在ASP.NET Core应用内能够直接使用,和旧的ASP.NET Cache API很是类似,它包含了大量零成本新特性,能够得到企业级的分布式缓存收益。记住一点,看在性能提高和扩展能力的份上,早用早享受。没有这些高级特性,那只能可怜地缓存一些简单数据。这份NCache caching features了解更多差异。架构
旧的ASP.NET的会话状态由框架提供,容许第三方组件以插件形式接入。在ASP.NET Core中的会话也是类似的,采用链式插件调用,有两种方式使用NCache做为ASP.NET Core的会话存储:
IDistributedCache Provider
存储配置若是把NCache做为ASP.NET Core的IDistributedCache provider
进行配置,NCache能自动的成为会话的默认存储方式,不用再有多余的操做。可是相比于旧的ASP.NET会话状态,这种方式能使用到的特性有限。
如下是ASP.NET Core默认会话缺失的地方:
自定义对象的byte
数组支持: ASP.NET Core强制你在把自定义对象存储到会话前先转换成byte数组。比起前一个方式,NCache已经实现了ASP.NET Core的Sessions Provider。这种方式拥有更多的特性能够利用。
如下是在Startup类进行配置的示例。
public class Startup { ... public void Configure(IApplicationBuilder app, IHostingEnvironment env) { ... app.UseNCacheSession(); ... } ... }
你能够在appsettings.json文件中作以下ASP.NET Core会话配置。
{ ... "NCacheSessions": { ... "CacheName": "demoCache", "EnableLogs": "True", "RequestTimeout": "90", "EnableDetailLogs": "False", "ExceptionsEnabled": "True", "WriteExceptionsToEventLog": "False" } ... }
在Configure NCache IDistributedCache Provider了解更多会话存储配置信息。
微软提供了IDistributedCache Providers
两种可选方案。一个是SQL Server另外一个是Redis。NCache比这两种方案更胜一筹表面上看是由于执行更快,更易扩展,固然还有如下缘由:
NCache更多详情:Nache Details Edition Comparison Download
(原文:How to Optimize ASP.NET Core Performance with Distributed Cache?)