这篇文章简单的讲解了response caching:

讲解了cache-control,及对其中的头和值的作用,及设置来控制response caching;

简单的罗列了其他的缓存技术:In-memory caching , Distributed Cache , Cache Tag Helper , Distributed Cache Tag Helper ;

讲解了使用ResponseCache attribute来控制response caching生成合适的头

主要翻译于官网,水平有限,见谅

Overview

响应缓存减少了客户端或者代理到web服务器的请求的次数。响应缓存也可以减少web服务器的生成响应的执行的工作量。响应缓存被头部控制,头部指出了你想要客户端,代理和中间件怎样缓存响应。

ResponseCache attribute 参加设置响应缓存头部,which clients may honor when caching responses. (当缓存响应时,客户端会受这些属性影响)。Response Caching Middleware 可以被用来在服务器上缓存响应。 中间件可以使用ResponseCacheAttribute属性来影响服务端缓存行为。

HTTP-based response caching 

HTTP 1.1 Caching specification(规格,详述,说明书)描述了网络缓存应该怎样表现(Internet caches should behave.) 。 主要的用于缓存的HTTP头,是Cache-Control, 它被用于指定缓存指令。这个指令控制缓存行为,当请求从客户端到服务端的时候,并且当响应从服务端返回客户端的时候。

公共的Cache-Control 指令在下表中被展示了:

其他缓存头在缓存中扮演的角色,罗列在下面了:

注意:Cache-Control,是用在从请求中的HTTP头,可以用来控制服务器中缓存行为。

HTTP-based caching respects request Cache-Control directives

HTTP 1.1 Caching specification for the Cache-Control header (HTTP 1.1 缓存规格对于Cache-Control头)要求一个缓存遵守一个有效的Cache-Control 头,这个Cache-Control头是被客户端发送的。一个客户端可以发送一个带no-cache的header值,并且强制要求服务器为每个请求生成一个新的响应。

总是遵守客户端Cache-Control请求头是有意义的,如果你考虑HTTP缓存的目标。在官方的说明书下,

缓存意味着减少潜在因素和网络管理,对满足请求跨客户端,代理和服务器网络。它不是一种控制原服务器上的加载的必须的方式。

当使用Response Caching 中间件时,开发者是没法对缓存行为控制的。因为中间件附着于官方缓存说明书。当决定提供一个缓存响应时,对这个中间件的计划丰富(planned enhancements to the middleware)对于达到配置中间件来忽视请求的Cache-Control头的目的,是一个机会(Planned enhancements to middleware are an opportunity to middleware to ignore a request’s Cache-Control header when deciding to serve a cached response.)。计划的丰富提供了一个机会来更好的控制服务器加载。

Other caching technology in ASP.NET Core 在ASP.NET Core上的其他缓存技术

  • In-memory caching 内存缓存

    In-memory caching 使用服务器内存来存储缓存数据。这种类型的缓存适合使用sticky session(sticky:不动的)的一个或者多个服务器。Sticky sessions 意味着从客户端发出的请求总是路由到同一台服务器处理。

    更多信息:Cache in-memory in ASP.NET Core.

  • Distributed Cache 分布式缓存

    使用一个分布式缓存来存储数据在内存中,当应用部署在云上或者服务器集群上时。缓存是在这些处理请求的服务器之间共享的。客户端可以提交一个请求,请求可以被组群里的任意服务器处理,如果缓存数据对于客户端是可用的。ASP.NET Core提供了SQL Server和Redis分布式缓存。

    更多信息:Distributed caching in ASP.NET Core.

  • Cache Tag Helper

    使用Cache Tag从mvc页面或者Razor Page中缓存内容。Cache Tag Helper用内存缓存数据。

    更多信息:Cache Tag Helper in ASP.NET Core MVC

  • Distributed Cache Tag Helper

    在分布式云或者web集群场景中使用Distributed Cache Tag Helper 来缓存Mvc view或者Razor Page中的内容。The Distributed Cache Tag Helper 用SQL Server或者Redis来缓存数据。

    更多信息:Distributed Cache Tag Helper in ASP.NET Core.

ResponseCache attribute

为了在response caching (响应缓存)上设置合适的头,ResponseCacheAttribute 指出了必须的参数。(即,可以通过ResponseCacheAttribute,设置response caching上的头的值)

注意:对于包含验证信息的客户端内容,不允许缓存。对于那些不会基于用户的身份或者用户是否登录而改变的内容,才应该允许被缓存。

VaryByQueryKeys 随着给出的query keys的集合的值,改变存储的响应。When a single value of * is provided, the middleware varies responses by all request query string parameters.

Response Caching Middleware 必须被允许设置VaryByQueryKeys属性。否则,一个运行时异常会被抛出。对于VaryByQueryKeys属性,并没有一个对应的HTTP头部。这个属性是一个被Response Caching Middleware 处理的HTTP 功能。对于中间件提供一个缓存的响应,查询字符串和查询字符串值必须匹配之前的请求.(即,如果查询字符串和查询字符串值和之前的一样时,中间件会直接返回一个缓存的响应;否则,返回一个新的响应。)例如,考虑下表中的一系列的请求和结果:

第一个请求被服务器返回,并且缓存到中间件中。第二个请求是被中间件返回,因为查询字符串匹配之前的请求。第三个请求不是在中间件缓存中的,因为查询字符串值不匹配之前的请求。

ResponseCacheAttribute用于配置和创建一个ResponseCacheFilter.

ResponseCacheFilter执行的工作,更新合适的HTTP头和响应的功能(即,ResponseCacheAttribute的功能)。The filter:

  • 移除任何存在的Vary, Cache-Control, Pragma头部
  • 根据设置在ResponseCacheAttribute中的属性输出合适的头部

  • 更新the response caching HTTP feature如果VaryByQueryKeys被设置了

Vary

这个头部会被写,当VaryByHeader属性被设置了。这个属性(VaryByHeader)设置Vary属性的值。下面是使用VaryByHeader属性的例子:

  1. [ResponseCache(VaryByHeader = "User-Agent", Duration = )]
  2. public class Cache1Model : PageModel
  3. {

用这个例子,使用浏览器工具观察response headers(响应头)。 下面的响应头随着Cache1 page response 被发送了。

  1. Cache-Control: public,max-age=
  2. Vary: User-Agent
NoStore and Location.None

NoStore重写了大部分的其他属性。当这个属性被设置为true,Cache-Control头被设置为no-store.

如果Location设置为None:

  • Cache-Control 设置为no-store, no-cache

  • Pragma设置为no-cache.

如果NoStore是false并且Location是None,Cache-Control ,和Pragma被设置为no-cache.

NoStore是典型的被设置为true,为了error pages. 示例中的Cache2 page生成响应头,指示客户端不要存储响应。

  1. [ResponseCache(Duration = , Location = ResponseCacheLocation.None, NoStore = true)]
  2. public class Cache2Model : PageModel
  3. {

这个示例应用返回Cache2 page 带有下面的头:

  1. Cache-Control: no-store,no-cache
  2. Pragma: no-cache
Location and Duration

为了可以缓存,Duration必须被设置为一个积极的值并且Location必须是任意的或者Client. 这种情况下,Cache-Control头被设置为location的值,并且跟着一个响应的max-age.

注意:

Location’s options of Any and Client转化为Cache-Control头的值分别为public和private. 正如之前提到的,设置Location为None会设置Cache-Control和Pramga头为no-cache:

  1. [ResponseCache(Duration = , Location = ResponseCacheLocation.Any, NoStore = false)]
  2. public class Cache3Model : PageModel
  3. {

示例应用返回的Cache3 page 带有下面的头:

  1. Cache-Control: public,max-age=
Cache profiles

取代重复的在很多controller action attributes响应缓存设置,cache profiles 可以被设置为options,当在Startup.ConfigureService中设置MVC/Razor Pages. 在引用的cache profiles中发现的值被用作默认值,随着ResponseCacheAttribute并且被这个attribute上指定的任意properties重写。(即很多重复的响应缓存设置可以在Startup.ConfigureService中设置,再随着ResponseCacheAttribute设置在action上)

建立一个cache profile. 下面的例子展示了30秒的cache profile,在示例应用的Startup.ConfigureServices:

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddMvc(options =>
  4. {
  5. options.CacheProfiles.Add("Default30",
  6. new CacheProfile()
  7. {
  8. Duration =
  9. });
  10. }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  11. }

这个示例应用的Cache4 page model 引用了Default30 cache profile:

  1. [ResponseCache(CacheProfileName = "Default30")]
  2. public class Cache4Model : PageModel
  3. {

这个ResponseCacheAttribute可以被用在:

  • Razor Page handlers(classes) - 属性可以被用到处理方法

  • MVC controllers(classes)

  • MVC actions (methods) - Method-level attributes override the settings specified in class level attributes. 方法级别的会覆盖类级别的

被Default30 profile导致的应用于Cache4 page response 的头是:

  1. Cache-Control: public,max-age=

下一篇:Cache in-memory

参考资料:

https://docs.microsoft.com/en-us/aspnet/core/performance/caching/response?view=aspnetcore-2.2

asp.net core 系列之Response caching(1)的更多相关文章

  1. asp.net core 系列之Response caching 之 Distributed caching(3)

    这篇文章讲解分布式缓存,即 Distributed caching in ASP.NET Core Distributed caching in ASP.NET Core 分布式缓存是可以在多个应用服 ...

  2. asp.net core 系列之Reponse caching 之 Response Caching Middleware(4)

    这篇文章介绍 Response Caching Middleware . Response Caching Middleware in ASP.NET Core 通过在ASP.NET Core应用中 ...

  3. asp.net core 系列之Reponse caching之cache in-memory (2)

    这篇文章(主要翻译于官网,水平有限,见谅)讲解asp.net core 中的 Cache in-memory (内存缓存). Cache in-memory in ASP.NET Core Cachi ...

  4. Ajax跨域问题及解决方案 asp.net core 系列之允许跨越访问(Enable Cross-Origin Requests:CORS) c#中的Cache缓存技术 C#中的Cookie C#串口扫描枪的简单实现 c#Socket服务器与客户端的开发(2)

    Ajax跨域问题及解决方案   目录 复现Ajax跨域问题 Ajax跨域介绍 Ajax跨域解决方案 一. 在服务端添加响应头Access-Control-Allow-Origin 二. 使用JSONP ...

  5. asp.net core系列 38 WebAPI 返回类型与响应格式--必备

    一.返回类型 ASP.NET Core 提供以下 Web API Action方法返回类型选项,以及说明每种返回类型的最佳适用情况: (1) 固定类型 (2) IActionResult (3) Ac ...

  6. asp.net core 系列 16 Web主机 IWebHostBuilder

    一.概述 在asp.net core中,Host主机负责应用程序启动和生存期管理.host主机包括Web 主机(IWebHostBuilder)和通用主机(IHostBuilder).Web 主机是适 ...

  7. asp.net core 系列 14 错误处理

    一.概述 本文介绍处理 ASP.NET Core 应用中常见错误的一些方法.主要是关于:开发环境异常页:非开发环境配置自定义异常处理页:配置状态代码页(没有正文响应,http状态400~599的). ...

  8. asp.net core 系列 8 Razor框架路由(下)

    三.页面路由操作约定 接着上篇讲asp.net core 系列 7 Razor框架路由.在上篇继续第三节 "页面路由操作约定" 的最后一小节 AddPageRoute . 3.3. ...

  9. 技术的正宗与野路子 c#, AOP动态代理实现动态权限控制(一) 探索基于.NET下实现一句话木马之asmx篇 asp.net core 系列 9 环境(Development、Staging 、Production)

    黄衫女子的武功似乎与周芷若乃是一路,飘忽灵动,变幻无方,但举手抬足之间却是正而不邪,如说周芷若形似鬼魅,那黄衫女子便是态拟神仙. 这段描写出自<倚天屠龙记>第三十八回. “九阴神抓”本是& ...

随机推荐

  1. Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】

    A. Flipping Game     time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. 分层图【p4568】 [JLOI2011]飞行路线

    Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在nn个城市设有业务,设这些城市分别标记为\(0\)到\(n−1\),一共有\(m\)种航线 ...

  3. Best Time to Buy and Sell Stock with Cooldown -- LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. unity3d Billboard

    CameraFacingBillboard   CameraFacingBillboard From Unify Community Wiki   Jump to: navigation, searc ...

  5. iOS静态库打包

    当了三年多程序员,第一次写随便,说来有些惭愧.想成为一个优秀的程序员,分析,思考,总结并且做记录是必不可少的,今天先从简单的命令总结记起.好了,废话不多说了,Let's start our games ...

  6. mysql悲观锁中的共享锁和排他锁

    概述: 共享锁又称为读锁,简称S锁,顾名思义,共享锁就是多个事务对于同一数据可以共享一把锁,都能访问到数据,但是只能读不能修改. 排他锁又称为写锁,简称X锁,顾名思义,排他锁就是不能与其他所并存,如一 ...

  7. UVa 1218 - Perfect Service

    /*---UVa 1218 - Perfect Service ---首先对状态进行划分: ---dp[u][0]:u是服务器,则u的子节点可以是也可以不是服务器 ---dp[u][1]:u不是服务器 ...

  8. git回退到某个commit

    git log查看提交历史及提交的commit_id 回退命令: $ git reset --hard HEAD^ 回退到上个版本$ git reset --hard HEAD~3 回退到前3次提交之 ...

  9. 对类中的成员函数的定义和声明最后添加一个const是什么意思?

    1.const修饰的成员函数只能调用const修饰的成员函数,且不能修改数据成员变量的值. 2.const修饰的类对象只能调用const修饰的成员函数. 3.const修饰的类对象可以调用非const ...

  10. hibernate中对象的3种状态:瞬时态(Transient)、 持久态(Persistent)、脱管态(Detached)

    Hibernate的对象有3种状态,分别为:瞬时态(Transient). 持久态(Persistent).脱管态(Detached). 处于持久态的对象也称为PO(Persistence Objec ...