0

我正在使用以下设置进行响应缓存:

 services.AddMvc(options =>
            {
                
                options.CacheProfiles.Add("HomePage", new CacheProfile()
                {
                    Duration = Constants.HomePageOutputCacheInSeconds,
                    Location = ResponseCacheLocation.Any,
                    VaryByHeader = HttpCacheProfileProvider.CacheKeyHeader

                });
                options.CacheProfiles.Add("Article", new CacheProfile()
                {
                    Duration = Constants.ArticleOutputCacheInSeconds,
                    Location = ResponseCacheLocation.Any,
                    VaryByHeader = HttpCacheProfileProvider.CacheKeyHeader
                });
                options.CacheProfiles.Add("Default", new CacheProfile()
                {
                    Duration = Constants.DefaultOutputCacheInSeconds,
                    Location = ResponseCacheLocation.Any,
                    VaryByHeader = HttpCacheProfileProvider.CacheKeyHeader
                });

                


            }).SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_2);

services.AddMemoryCache();
services.AddResponseCaching();

在配置部分我设置:

 app.UseResponseCaching();

和控制器:

[Route("", Name = "DesktopHome")]
[ResponseCache(CacheProfileName = "HomePage", Order = int.MaxValue)]
public async Task<IActionResult> Index()

一切正常。Cache-Control:public, max-age:10附加在标题中,但我也想设置 must-revalidate 和 max-stale-cache 属性,但我找不到属性来完成它。

属性在 CacheProfiles 设置、ResponseCacheAttribute 和 app.UseResponseCaching 设置中均不可用。

那可能吗?

4

1 回答 1

1

除了在 Cacheprofile 中设置外,您还可以直接使用 Response 来设置它们:

    public async Task<IActionResult> Index()
    {
        Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue()
        {
            Public = true,
            MaxAge = TimeSpan.FromSeconds(600),
            MustRevalidate = true,
            MaxStale = true
        };

        return View();
    }

在此处输入图像描述

或者您可以在官方网站上查看响应缓存中间件。

于 2021-05-28T09:28:07.463 回答