不同 ASP.NET 缓存选项的优缺点 [英] Pros/Cons of different ASP.NET Caching Options

查看:23
本文介绍了不同 ASP.NET 缓存选项的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我遇到了:

  • 内存缓存

  • Memory Cache

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

使用静态成员变量:

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);
    

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

    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?

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

    推荐答案

    每种缓存技术/方法都有自己的一组功能.这些功能在一种应用程序需求中似乎是不利的,但在其他应用程序需求中可能是有利的.

    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.

    例如,让我们讨论一些客户端缓存技术.

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

    MSDN says that we can also use HiddenField to 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.

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

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

    但是,MSDN 明确表示:这种方法的安全支持最少.

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

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

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

    再看一个例子页面输出缓存:它是2种类型,页面输出缓存和页面片段缓存.

    Consider one more example, Page Output caching : it is of 2 types, page output caching and page fragment 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.

    最后一个评论 Application vs HttpRuntime.cache:

    Application 不是缓存,它是一个全局命名值集合.如果您将对象添加到 Application,它将一直保留到应用程序域回收.

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

    • 应用程序变量是网络应用程序的所有用户之间的共享变量
    • 应用程序变量的行为类似于静态变量,它们可以替代静态变量,因为静态变量在 Web 应用程序中是无状态的
    • 只有共享值应该保留在应用程序变量中,并且一旦它们不使用,就应该明确删除它们.

    Cache :通过在 ApplicationCache类.虽然 Cache 类确实提供了更多的灵活性和控制,但它似乎只提供了比用于缓存的 Application 类增加的吞吐量的边际优势.开发一种测试方案来准确衡量 Cache 类通过清理过程对较少使用的对象的内置管理的潜在优势是非常困难的,这与 Application 不这样做的事实相反提供此功能.在这种情况下,开发人员需要做出决定,并应基于项目的需求和便利性及其使用模式.查看此链接了解更多信息.

    Cache :It is possible to obtain significant performance improvements in ASP.NET applications by caching frequently requested objects and data in either the Application or Cache classes. While the Cache class certainly offers far more flexibility and control, it only appears to offer a marginal advantage in terms of increased throughput over the Application class for caching. It would be very difficult to develop a testing scheme that could accurately measure the potential advantages of the Cache class'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 link for more.

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

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

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

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

    这篇关于不同 ASP.NET 缓存选项的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆