【发布时间】:2021-05-05 05:43:30
【问题描述】:
我们正在使用 Azure API 管理来发布、监控和维护 API。我们还为身份验证和授权实现了 B2C 登录。
我正在尝试为 API 配置外部缓存。不知何故,缓存策略不起作用。我参考以下链接https://docs.microsoft.com/en-us/azure/api-management/api-management-sample-cache-by-key
基于登录的用户租户 ID,我们希望将模板存储在缓存中,并稍后检索以供下一个请求。 这是我写的政策。
<policies>
<inbound>
<set-variable name="tenantId" value="@(context.Request.Headers.GetValueOrDefault("Authorization","").Split(' ')[1].AsJwt()?.Subject)" />
<cache-lookup-value key="@("templates-" + context.Variables["tenantId"])" variable-name="templates" />
<choose>
<when condition="@(!context.Variables.ContainsKey("tenantId"))">
<send-request mode="new" response-variable-name="templateResponse" timeout="15" ignore-error="true">
<set-url>https://abc.azure-api.net/api/notification/templates/?api-version=v1</set-url>
<set-method>GET</set-method>
</send-request>
<set-variable name="templates" value="@(((IResponse)context.Variables["templateResponse"]).Body.As<string>())" />
<cache-store-value key="@("templates-" + context.Variables["tenantId"])" value="@((string)context.Variables["templates"])" duration="10000" />
</when>
</choose>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
【问题讨论】:
标签: azure azure-api-management api-management