http响应缓存可减少客户端或代理对web服务器发出的请求数。响应缓存还减少了web服务器生成响应所需的工作量。响应缓存由http请求中的header控制。

asp.net core对其都有相应的实现,并不需要了解里面的工作细节,即可对其进行良好的控制。

了解http缓存

http协议中定义了许多缓存,但总体可以分为强缓存和协商缓存两类。

强缓存

强缓存是指缓存命中时,客户端不会向服务器发请求,浏览器f12能看到响应状态码为200sizefrom cache,它的实现有以下几种方式:

expires – 绝对时间

示例:expires:thu,31 dec 2037 23:59:59 gmt,就表示缓存有效期至2037年12月31日,在这之前浏览器都不会向服务器发请求了(除非按f5/ctrl+f5刷新)。

cache-control – 相对时间/更多控制

绝对时间是一个绝对时间,因为计算时不方便;而且服务端是依据服务器的时间来返回,但客户端却需要依据客户的时间来判断,因此也容易失去控制。

cache-control有以下选项(可以多选):

  1. max-age: 指定一个时间长度,在这个时间段内缓存是有效的,单位是秒(s)。例如设置cache-control:max-age=31536000,也就是说缓存有效期为31536000/24/60/60=365天。
  2. s-maxage: 同max-age,覆盖max-age、expires,但仅适用于共享缓存,在私有缓存中被忽略。
  3. public: 表明响应可以被任何对象(发送请求的客户端、代理服务器等等)缓存。
  4. private: 表明响应只能被单个用户(可能是操作系统用户、浏览器用户)缓存,是非共享的,不能被代理服务器缓存。
  5. no-cache: 强制所有缓存了该响应的用户,在使用已缓存的数据前,发送带验证器的请求到服务器。(不是字面意思上的不缓存)
  6. no-store: 禁止缓存,每次请求都要向服务器重新获取数据。
  7. must-revalidate: 指定如果页面是过期的,则去服务器进行获取。(意思是浏览器在某些情况下,缓存失效后仍可使用老缓存,加了这个头,失效后就必须验证,并不是字面上有没有过期都验证)

其中最有意思的要数no-cachemust-revalidate了,因为它们的表现都不是字面意义。

no-cache并不是字面上的不缓存,而是会一直服务端验证(真实意义很像字面上的must-revalidate)。

must-revalidate是只是为了给浏览器强调,缓存过期后,千万要遵守约定重新验证。

协商缓存

协商缓存是指缓存命中时,服务器返回http状态码为304但无内容(body),没命中时返回200有内容。

在要精细控制时,协商缓存比强缓存更有用,它有last-modifiedetag两种。

last-modified/if-modify-since(对比修改时间)

示例:

服务器:last-modified: sat, 27 jun 2015 16:48:38 gmt
客户端:if-modified-since: sat, 27 jun 2015 16:48:38 gmt

etag/if-none-match(对比校验码)

服务器:etag: w/”0a0b8e05663d11:0″
客户端:if-none-match: w/”0a0b8e05663d11:0″

清缓存要点

  1. f5刷新时,强缓存失效
  2. ctrl+f5刷新时 强缓存和协商缓存都失效

asp.net core的http缓存

asp.net core中提供了responsecacheattribute来实现缓存,它的定义如下:

[attributeusage(attributetargets.class | attributetargets.method, allowmultiple = false, inherited = true)]
public class responsecacheattribute : attribute, ifilterfactory, ifiltermetadata, iorderedfilter
{
  public responsecacheattribute();
  public string cacheprofilename { get; set; }
  public int duration { get; set; }
  public bool isreusable { get; }
  public responsecachelocation location { get; set; }
  public bool nostore { get; set; }

  public int order { get; set; }
  public string varybyheader { get; set; }
  public string[] varybyquerykeys { get; set; }
}

其中,responsecachelocation定义了缓存的位置,是重点:

//   determines the value for the "cache-control" header in the response.
public enum responsecachelocation
{
  //   cached in both proxies and client. sets "cache-control" header to "public".
  any = 0,
  //   cached only in the client. sets "cache-control" header to "private".
  client = 1,
  //   "cache-control" and "pragma" headers are set to "no-cache".
  none = 2
}

注意看源文件中的注释,any表示cache-control: publicclient表示cache-control: privatenone表示cache-control: no-cache

注意responsecachelocation并没有定义将缓存放到服务器的选项。

其中duration表示缓存时间,单位为秒,它将翻译为max-age

另外可以通过varybyheadervarybyquerykeys来配置缓存要不要通过headerquery string来变化,其中varybyheader是通过http协议中的vary头来实现的,varybyquerykeys必须通过middleware来实现。

不要误会,所有responsecacheattribute的属性配置都不会在服务端缓存你的响应数据(虽然你可能有这种错觉),它和输出缓存不同,它没有状态,只用来做客户端强缓存。

如果不想缓存,则设置nostore = true,它会设置cache-control: no-store,我们知道no-store的真实意思是不缓存。一般还会同时设置location = responsecachelocation.none,它会设置cache-control: no-cache(真实意思是表示一定会验证)。

注意单独设置location = responsecachelocation.none而不设置nostore并不会有任何效果。

示例1

这是一个很典型的使用示例:

public class homecontroller : controller
{
  [responsecache(duration = 3600, location = responsecachelocation.client)]
  public iactionresult data()
  {
    return json(datetime.now);
  }
}

我定义了3600秒的缓存,并且cache-control应该为private,生成的http缓存头可以通过如下c#代码来验证:

using var http = new httpclient();
var resp1 = await http.getasync("https://localhost:55555/home/data");
console.writeline(resp1.headers.cachecontrol.tostring());
console.writeline(await resp1.content.readasstringasync());

输入结果如下:

max-age=3600, private
“2020-03-07t21:35:01.5843686+08:00”

另外,responsecacheattribute也可以定义在controller级别上,表示整个controller都受到缓存的影响。

cacheprofilename示例

另外,如果需要共用缓存配置,可以使用cacheprofilename,将缓存提前定义好,之后直接传入这个定义名即可使用:

.configureservices(s =>
{
  s.addcontrollers(o =>
  {
    o.cacheprofiles.add("3500", new cacheprofile
    {
      duration = 3500, 
      location = responsecachelocation.client,
    });
  });
});

这样我就定义了一个名为3500的缓存,稍后在controller中我只需传入cacheprofilename = 3500即可:

public class homecontroller : controller
{
  [responsecache(cacheprofilename = "3500")]
  public iactionresult data()
  {
    return json(datetime.now);
  }
}

总结

http缓存分为强缓存和协商缓存,asp.net core提供了便利的responsecacheattribute实现了强缓存,还能通过profile来批量配置多个缓存点。

asp.net mvc并没有提供协商缓存实现,因为这些多半和业务逻辑相关,需要自己写代码。静态文件是特例,microsoft.aspnetcore.staticfiles中提供有,因为静态文件的逻辑很清晰。

asp.net中的outputcacheattributeasp.net core中不复存在,取而代之的是app/services.addresponsecaching(),这些和http协议不相关。

有机会我会具体聊聊这些缓存。

到此这篇关于asp.net core中的http缓存使用的文章就介绍到这了,更多相关asp.net core http缓存内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!