如果您的 ASP.NET 应用程序只有几个用户,那么您可能不在乎它的速度有多快或多慢,而且它可能会为您提供相当不错的性能。 但是,当您向 ASP.NET 应用程序添加更多负载时,ASP.NET 性能显着下降的可能性非常高。 如果施加足够的负载,它甚至可能会停止运转。 而且,具有讽刺意味的是,所有这些都发生在您的企业看到更多活动时,因此影响更大。
如今,ASP.NET 在高流量应用程序中变得非常流行,现在常见的是 10-20 个服务器负载平衡的 Web 场,在某些情况下甚至 50-100 个服务器场。 因此,在这些情况下,ASP.NET 的性能问题是需要解决的更为敏感的问题。
NCache 更多信息 NCache 配套文档 ASP.NET 缓存文档
运用 NCache 作为分布式缓存
随着负载的增加,ASP.NET 性能下降的主要原因是您的数据库无法像 ASP.NET 应用程序 Web 场那样处理更大的负载。 这是因为您可以向 ASP.NET Web 场添加更多服务器,但不能对数据库执行相同操作。
因此,在这些情况下,您最好的选择是使用 分布式缓存 喜欢 NCache. NCache 是在内存中的,所以它比数据库快得多。 和, NCache 构建一个缓存服务器集群,你可以像网络农场一样线性增长集群。 结果,随着 NCache,即使在极端事务负载下,您的 ASP.NET 性能仍然很好。 您可以使用 NCache 通过以下两种方式:
ASP.NET 会话状态存储
您可以将 ASP.NET 应用程序配置为 将 ASP.NET 会话状态存储在 NCache 而不是 InProc、State Server 或 SQL Server。 请注意,这里不需要编程。 您只需修改 web.config 代码如下:
1 2 3 4 5 6 |
<sessionstate cookieless="false" regenerateexpiredsessionid="true" mode="Custom" customprovider="NCacheSessionProvider" timeout="20"> <providers> <add name="NCacheSessionProvider" type="Alachisoft.NCache.Web.SessionState.NSessionStoreProvider" exceptionsenabled="true" enablesessionlocking="true" emptysessionwhenlocked="false" sessionlockingretry="-1" sessionappid="NCacheTest" useinproc="false" enablelogs="false" cachename="myReplicatedCache" writeexceptionstoeventlog="false" asyncsession="false"> <providers> <sessionstate></sessionstate></providers></add></providers></sessionstate> |
ASP.NET 应用程序数据缓存
另一种方法是让你 缓存应用程序数据 在分布式缓存中 NCache 所以下次你的 ASP.NET 应用程序需要这个数据时,它会在缓存中找到它。 这是一个关于如何缓存应用程序数据的小代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using Alachisoft.NCache.Web.Caching; ... Cache cache = NCache.InitializeCache("myCache"); // Create a key to lookup in the cache // The key for will be like “Employees:PK:1000” string key = "Employee:EmployeeId:" + emp.EmployeeId.ToString(); Employee employee = (Employee)Cache[key]; if (employee == null) { // item not found in the cache. load from db LoadEmployeeFromDb(employee); // Now, add it to the cache for future reference Cache.Insert(key, employee, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null ); } |
您缓存的数据越多,访问数据库的次数就越少,您的 ASP.NET 应用程序性能也就越快。