【发布时间】:2020-04-13 15:37:05
【问题描述】:
目标:使用 API 管理缓存策略来缓存一个永远不会改变的 json 响应。
原始(无缓存)
此策略由一个 azure 函数支持,该函数返回一个永不更改的 json 响应。
<policies>
<inbound>
<base />
<set-backend-service id="apim-generated-policy" backend-id="azfunc-fluffyoauth2" />
<rewrite-uri template="/WellKnownOpenidConfiguration" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
考虑到这可能是缓存的好东西,我介绍了缓存查找策略。
<policies>
<inbound>
<base />
<set-backend-service id="apim-generated-policy" backend-id="azfunc-fluffyoauth2" />
<rewrite-uri template="/WellKnownOpenidConfiguration" />
<cache-lookup vary-by-developer="false" vary-by-developer-groups="false" downstream-caching-type="none" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<cache-store duration="36000" />
</outbound>
<on-error>
<base />
</on-error>
</policies>
我看到的是缓存版本返回一个我认为是 gzip 压缩的二进制文件。这会导致预期它是原始 json 的下游代码中断。这个响应是一个 openid 连接发现文档,具有讽刺意味的是,如果我将 API Management 的 jwt-policy 指向它,它也会中断。
即
var auth0Domain = "https://apim-mycompany.azure-api.net/oauth2";
string responseString = null;
try
{
var url = $"{auth0Domain}/.well-known/openid-configuration-cached";
Console.WriteLine($"-------------------");
Console.WriteLine($"{url}");
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(new Uri($"{auth0Domain}/.well-known/openid-configuration-cached"));
responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine($"-------------------");
Console.WriteLine($"{responseString}");
Console.WriteLine($"-------------------");
JsonConvert.DeserializeObject<OpenIdConnectConfiguration>(responseString);
}
catch (Exception ex)
{
Console.WriteLine($"Error:{ex.Message}");
Console.WriteLine($"-------------------");
return;
}
curl -I --location --request GET 'https://apim-fluffyoauth2.azure-api.net/oauth2/.well-known/openid-configuration-cached'
HTTP/1.1 200 OK
Cache-Control: no-store, must-revalidate, no-cache
Pragma: no-cache
Content-Length: 558
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
Expires: Tue, 14 Apr 2020 18:50:53 GMT
Vary: Accept-Encoding
Request-Context: appId=cid-v1:dfeba42c-e636-42bc-b501-7c77563c3e7b,appId=cid-v1:dfeba42c-e636-42bc-b501-7c77563c3e7b
Date: Tue, 14 Apr 2020 18:50:53 GMT
问题:
缓存策略在做什么?
如何将其更改为仍缓存但响应非缓存版本的功能?
【问题讨论】:
-
响应本身被 gzip 压缩不应该是一个问题,只要所有内容标题都到位。您能否添加一个包含从 APIM 获得的所有标头的示例缓存响应?
-
APIM 会突然不做 gzip,我所要做的就是修改缓存策略 TTL 并保存。这会触发它开始发送 gzip。 apim-fluffyoauth2.azure-api.net/oauth2/.well-known/… HTTP/1.1 200 OK Cache-Control: no-store, must-revalidate, no-cache Pragma: no-cache Content-Length: 558 Content-Type: application/json; charset=utf-8 内容编码:gzip 过期:格林威治标准时间 2020 年 4 月 14 日星期二 18:50:53 变化:接受编码日期:格林威治标准时间 2020 年 4 月 14 日星期二 18:50:53
-
APIM 总是将所有客户端标头发送到后端,您是否发送了接受编码?您可能希望在缓存查找策略中添加 vary="accept-encoding",如果客户端没有要求,这将阻止 APIM 回复缓存的 gzip 内容。
-
我添加了
Accept 和 Accept-Charset。这似乎已经解决了。你得到你想要的。 接受 接受字符集