国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - ASP.NET Core中的Http緩存使用

ASP.NET Core中的Http緩存使用

2020-06-27 16:04.NET騷操作 ASP.NET教程

這篇文章主要介紹了ASP.NET Core中的Http緩存使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

Http響應緩存可減少客戶端或代理對web服務器發出的請求數。響應緩存還減少了web服務器生成響應所需的工作量。響應緩存由Http請求中的header控制。

ASP.NET Core對其都有相應的實現,并不需要了解里面的工作細節,即可對其進行良好的控制。

了解Http緩存

Http協議中定義了許多緩存,但總體可以分為強緩存和協商緩存兩類。

ASP.NET Core中的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來實現緩存,它的定義如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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定義了緩存的位置,是重點:

?
1
2
3
4
5
6
7
8
9
10
//   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

這是一個很典型的使用示例:

?
1
2
3
4
5
6
7
8
public class HomeController : Controller
{
  [ResponseCache(Duration = 3600, Location = ResponseCacheLocation.Client)]
  public IActionResult Data()
  {
    return Json(DateTime.Now);
  }
}

我定義了3600秒的緩存,并且cache-control應該為private,生成的Http緩存頭可以通過如下C#代碼來驗證:

?
1
2
3
4
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,將緩存提前定義好,之后直接傳入這個定義名即可使用:

?
1
2
3
4
5
6
7
8
9
10
11
.ConfigureServices(s =>
{
  s.AddControllers(o =>
  {
    o.CacheProfiles.Add("3500", new CacheProfile
    {
      Duration = 3500,
      Location = ResponseCacheLocation.Client,
    });
  });
});

這樣我就定義了一個名為3500的緩存,稍后在Controller中我只需傳入CacheProfileName = 3500即可:

?
1
2
3
4
5
6
7
8
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緩存內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://www.cnblogs.com/sdflysha/p/20200301-http-cache-in-aspnet-core.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲精品久久久久久国产 | 久久综合久色欧美综合狠狠 | a级黄色在线观看 | 天堂在线视频 | 免费一级视频在线观看 | 黄色av网站在线观看 | 国产一级一级国产 | 中文字幕一区在线观看视频 | 91精品国产综合久久香蕉最新版 | 欧美爱爱视频 | 成人激情在线播放 | 日韩免费一区 | 亚洲高清视频在线 | 欧美精品网站 | 精品在线一区二区三区 | 久久a视频 | 成年人黄色免费网站 | 在线日韩一区二区 | av片免费 | 国产精品a久久 | 九一视频在线免费观看 | 日韩欧美在线观看一区二区 | 亚洲视频中文字幕在线观看 | www.欧美日韩 | 亚洲一区高清 | 久久久精品视频国产 | 91 在线| 国产高清视频一区 | 久久精品久久久 | 亚洲网站在线观看 | 激情五月综合网 | 成人在线国产 | 免费毛片网站 | 中文字幕电影在线 | 91在线看 | 日本精品在线观看 | 精品久久久久久亚洲综合网 | 国产精品久久精品 | 亚洲精品视频在线 | 久久国内精品 | 正在播放国产精品 |