【问题标题】:Difference between TryGetValue() and GetorCreate()?TryGetValue() 和 GetorCreate() 的区别?
【发布时间】:2018-11-18 17:38:15
【问题描述】:

我正在使用 ASP.NET MVC 和 Angular 制作一个 Web 应用程序。为了缓存主仪表板的结果,我使用了内置的 IMemoryCache。

我将 GetorCreate 用于其 Async 版本,但 TryGetValue() 和 GetorCreate() 之间的真正区别是什么?他们不是都尝试从缓存中获取值,但如果不是我们可以做到吗?

【问题讨论】:

    标签: asp.net angularjs asp.net-mvc-4 caching


    【解决方案1】:

    也许将来这对某人有用。

    GetOrCreate(): 通过使用 cache.GetOrCreate() 可以检查传递的键是否存在,如果不存在则 将创建新的缓存键值。

    cache.GetOrCreate()<string>(“username”, entry => { return “admin”; });
    

    TryGetValue():通过使用cache.TryGetValue()我们可以检查key是否存在。这里的主要区别 cache.GetOrCreate() 和 cache.TryGetValue() 方法就是 TryGet() 方法只会检查密钥是否存在,而 GetOrCreate() 方法将缓存密钥是否存在以及是否 它不存在然后可以创建一个新的。

    cache.TryGetValue()<string>(“username”, out string username);
    

    但两种方法之间并没有太大区别

    链接: https://www.itpathsolutions.com/know-about-in-memory-caching-asp-net-core/ https://docs.microsoft.com/en-US/aspnet/core/performance/caching/memory?view=aspnetcore-3.1#cache-guidelines

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 2011-10-20
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      相关资源
      最近更新 更多