C# 不同 ASP.NET 缓存选项的优缺点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18937855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:39:00  来源:igfitidea点击:

Pros/Cons of different ASP.NET Caching Options

c#asp.netasp.net-mvccachingasp.net-web-api

提问by vesuvious

I recently asked a question about caching application data in an ASP.NET MVC WebAPI application and it led me to a new question. What are the pros/cons of different caching methods available in ASP.NET?

我最近问了一个关于在 ASP.NET MVC WebAPI 应用程序中缓存应用程序数据的问题,它让我想到了一个新问题。ASP.NET 中可用的不同缓存方法的优缺点是什么?

I have come upon:

我遇到了:

  • Memory Cache

    http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

  • Using Static Member Variables:

    private static Northwind.SuppliersDataTable suppliers = null;
    
  • Application State:

     HttpContext.Current.Application["key"] ="Value"
    
  • Data Cache:

    HttpRuntime.Cache.Insert(
      /* key */                "key", 
      /* value */              "value", 
      /* dependencies */       null, 
      /* absoluteExpiration */ Cache.NoAbsoluteExpiration, 
      /* slidingExpiration */  Cache.NoSlidingExpiration, 
      /* priority */           CacheItemPriority.NotRemovable, 
      /* onRemoveCallback */   null);
    
  • 内存缓存

    http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

  • 使用静态成员变量:

    private static Northwind.SuppliersDataTable suppliers = null;
    
  • 应用状态:

     HttpContext.Current.Application["key"] ="Value"
    
  • 数据缓存:

    HttpRuntime.Cache.Insert(
      /* key */                "key", 
      /* value */              "value", 
      /* dependencies */       null, 
      /* absoluteExpiration */ Cache.NoAbsoluteExpiration, 
      /* slidingExpiration */  Cache.NoSlidingExpiration, 
      /* priority */           CacheItemPriority.NotRemovable, 
      /* onRemoveCallback */   null);
    

I am sure there are others, and I know they all technically store the data in memory...so any idea what I should use for an ASP.NET MVC webapi?

我确定还有其他人,我知道他们在技术上都将数据存储在内存中......所以知道我应该为 ASP.NET MVC webapi 使用什么吗?

My Previous Question: Caching application data in memory: MVC Web API

我以前的问题: 在内存中缓存应用程序数据:MVC Web API

采纳答案by R.C

Each Caching technology/methods have their own set of features. These features may seem to be of disadvantage in one application requirements but can be advantageous in other application requirements.

每个缓存技术/方法都有自己的一组功能。这些特征在一种应用要求中似乎是不利的,但在其他应用要求中可能是有利的。

So, in short , depending on your requirements decide which Caching technology and what features are best for you.

因此,简而言之,根据您的要求决定哪种缓存技术和哪些功能最适合您。

For example, Let us discuss some client side Caching techniques.

For example, Let us discuss some client side Caching techniques.

MSDN says that we can also use HiddenFieldto store only small amounts of frequently changing data in hidden fields because this data is included in the roundtrips to the server on every postback.

MSDN 说,我们还可以使用HiddenField在隐藏字段中仅存储少量频繁更改的数据,因为这些数据包含在每次回发到服务器的往返行程中。

Advantage of this feature:Reduces the workload on your server by storing page information using client-side options.

此功能的优点:通过使用客户端选项存储页面信息来减少服务器上的工作量。

However, MSDN says clearly that : This approach has minimal security support.

但是,MSDN 明确表示: 这种方法具有最低限度的安全支持。

Thus, one may or may not use this feature always as security considerations are also there.

因此,出于安全考虑,人们可能会或可能不会始终使用此功能。

Consider one more example, Page Output caching: it is of 2 types, page output caching and page fragment caching.

Consider one more example, Page Output caching: 它有两种类型,页面输出缓存和页面片段缓存。

Page output caching caches an entire Web page and is suitable only when the content of that page is fairly static. If parts of the page are changing, you can wrap the static sections as user controls and cache the user controls using page fragment caching.

页面输出缓存缓存整个 Web 页面,并且仅适用于该页面的内容相当静态的情况。如果页面的某些部分发生变化,您可以将静态部分包装为用户控件并使用页面片段缓存来缓存用户控件。

And one last comment onApplicationvs HttpRuntime.cache:

And one last comment onApplication对比HttpRuntime.cache

Applicationis not a cache, its a global named value collection. if you add an object to Applicationit will stay until a an appdomain recycle.

Application不是缓存,它是一个全局命名值集合。如果你向Application它添加一个对象,它会一直保留到 appdomain 回收。

  • Application variables are shared variables among all users of a web application
  • Application variables behave like static variables and they are substitute of static variables as static variables are stateless in web applications
  • Only shared values should be persisted in Application variables, and as soon as they are not in use they should be removed explicitly.
  • 应用程序变量是 Web 应用程序的所有用户之间的共享变量
  • 应用程序变量的行为类似于静态变量,它们替代静态变量,因为静态变量在 Web 应用程序中是无状态的
  • 只有共享值应该保留在应用程序变量中,并且一旦它们不被使用,就应该明确地删除它们。

Cache:It is possible to obtain significant performance improvements in ASP.NET applications by caching frequently requested objects and data in either the Applicationor Cacheclasses. While the Cacheclass certainly offers far more flexibility and control, it only appears to offer a marginal advantage in terms of increased throughput over the Applicationclass for caching. It would be very difficult to develop a testing scheme that could accurately measure the potential advantages of the Cacheclass's built - in management of lesser-used objects through the scavenging process as opposed to the fact that Application does not offer this feature. The developer needs to take decision in this case and should be based on the needs and convenience of the project and its usage patterns. Check this linkfor more.

Cache:通过在ApplicationCache类中缓存频繁请求的对象和数据,可以在 ASP.NET 应用程序中获得显着的性能改进。虽然Cache该类确实提供了更多的灵活性和控制,但它似乎只提供了比Application缓存类更高的吞吐量方面的边际优势。Cache与 Application 不提供此功能的事实相反,开发一种可以准确衡量类的内置管理潜在优势的测试方案将非常困难,即通过清理过程对较少使用的对象进行管理。在这种情况下,开发人员需要做出决定,并应基于项目的需求和便利性及其使用模式。检查此链接更多。

Refer this MSDN articlefor a complete great explanation on all Caching technologies in Asp.net with discusiion on the features of each technology.

有关 Asp.net 中所有缓存技术的完整解释,并讨论每种技术的特性,请参阅这篇 MSDN 文章

Also, these 2 links are a great source to start with:

此外,这两个链接是一个很好的起点:

回答by Darrel Miller

When using Web API your first choice for caching should always be to set the caching headers in the HTTP response. HttpResponseMessage.CacheControlHeader.

使用 Web API 时,缓存的首选应该始终是在 HTTP 响应中设置缓存标头。 HttpResponseMessage.CacheControlHeader.

Your last options should be anything that depends on HttpContextor HttpRuntime, as that will tie you to particular hosts. Web API applications should be built independent of their host.

您的最后一个选项应该是依赖于HttpContext或 的任何选项HttpRuntime,因为这会将您与特定主机联系起来。Web API 应用程序应该独立于它们的主机构建。

回答by Joe

Regarding MemoryCachevs ASP.NET Cache: they provide very similar functionality. In an ASP.NET 4 application, I'd generally prefer the ASP.NET Cache, if for no other reason, then because of a bug in .NET 4, which is apparently fixed in .NET 4.5.

关于MemoryCachevs ASP.NET 缓存:它们提供非常相似的功能。在 ASP.NET 4 应用程序中,我通常更喜欢 ASP.NET 缓存,如果没有其他原因,那么是因为.NET 4 中一个错误该错误显然已在 .NET 4.5 中修复。

Static fields are appropriate to store shared data that does not need an expiration policy.

静态字段适用于存储不需要过期策略的共享数据。

Application state isn't much more than a static dictionary with locking semantics that is compatible with classic ASP - I'd only use it for backwards compatibility with legacy classic ASP code.

应用程序状态只不过是一个具有与经典 ASP 兼容的锁定语义的静态字典 - 我只会使用它来向后兼容旧的经典 ASP 代码。