【问题标题】:Performance improve in ASP.NET Through Aplication Level Cache?通过应用程序级缓存提高 ASP.NET 中的性能?
【发布时间】:2020-08-27 08:34:22
【问题描述】:

我开发了一个 dotnet 应用程序,它包含大量数据。由于数据量大,应用程序消耗更多的带宽和缓慢的速度。有没有办法通过应用程序级缓存来提高性能。我在使用 sql server 时研究了 redis 但它的键值。我想要一个解决方案,以便将表从 sql 服务器加载到 Web 服务器缓存,这样它就不必一次又一次地访问 sql,谢谢。

【问题讨论】:

  • 根据您的描述,您的应用程序是dotnet 应用程序,而不是dotnet 核心应用程序,对吧?如果是这种情况,我们可以缓存静态内容,使用 OutputCache 属性缓存整个或部分页面响应并缓存共享数据,更多详细信息请参阅Three Ways To Improve Performance Using Caching In ASP.NET MVC Applications。另外,对于大数据的展示,也可以尝试使用分页。
  • 它的 dotnet 核心 Web 应用程序,带有 Angular 前端。

标签: .net asp.net-mvc asp.net-core asp.net-web-api


【解决方案1】:

ASP.NET Core 支持多种不同的缓存。最简单的缓存是基于IMemoryCache

要在 Asp.net core 中使用 In-Memory Cache,你可以使用System.Runtime.Caching/MemoryCache (NuGet package),然后,你可以在 ConfigureServices 中注册 IMemoryCache,使用:

services.AddMemoryCache();

然后,在控制器中,您可以参考以下代码使用缓存来存储数据。

public class HomeController : Controller
{
    private IMemoryCache _cache;

    public HomeController(IMemoryCache memoryCache)
    {
        _cache = memoryCache;
    }
    public IActionResult CacheTryGetValueSet()
    {
         DateTime cacheEntry;

        // Look for cache key.
        if (!_cache.TryGetValue(CacheKeys.Entry, out cacheEntry))
        {
            // Key not in cache, so get data.
            cacheEntry = DateTime.Now;

            // Set cache options.
            var cacheEntryOptions = new MemoryCacheEntryOptions()
                // Keep in cache for this time, reset time if accessed.
                .SetSlidingExpiration(TimeSpan.FromSeconds(3));

            // Save data in cache.
            _cache.Set(CacheKeys.Entry, cacheEntry, cacheEntryOptions);
        }

        return View("Cache", cacheEntry);
    }

更多详情,请查看Cache in-memory in ASP.NET Core

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-12
    • 2011-04-04
    • 2011-07-28
    • 2012-11-30
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多