简体   繁体   English

Azure API 管理配置外部缓存策略

[英]Azure API management configuring external caching policy

We are using Azure API Management to publish, monitor and maintain APIs.我们正在使用 Azure API 管理来发布、监控和维护 API。 Also we have implemented B2C login for Authentication and Authorization.我们还为身份验证和授权实现了 B2C 登录。

I'm trying to configure external cache for APIs.我正在尝试为 API 配置外部缓存。 Somehow caching policy is not working.不知何故,缓存策略不起作用。 I refer following link https://docs.microsoft.com/en-us/azure/api-management/api-management-sample-cache-by-key我参考以下链接https://docs.microsoft.com/en-us/azure/api-management/api-management-sample-cache-by-key

Based on logged in user tenant id, we want to store the template in cache and retrieve later for next request.根据登录的用户租户 ID,我们希望将模板存储在缓存中,稍后检索以供下一个请求。 Here is the policy I have written.这是我写的政策。

<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>

As long as there is an authorization header with the request then APIM will not cache the request unless you enable the private response caching.只要请求有授权 header ,那么 APIM 就不会缓存请求,除非您启用私有响应缓存。

You can set the allow-private-response-caching attribute to ture in order to enable private response caching https://docs.microsoft.com/en-us/azure/api-management/api-management-caching-policies#elements .您可以将 allow-private-response-caching 属性设置为 ture 以启用私有响应缓存https://docs.microsoft.com/en-us/azure/api-management/api-management-caching-policies#elements .

if with that it does not continue to work then I recommend file a support ticket to the MS team.如果这样它不能继续工作,那么我建议向 MS 团队提交支持票。

I made some mistake while writing policy.我在写政策时犯了一些错误。 Here is the corrected policy.这是更正后的政策。

<policies>
<inbound>
    <set-variable name="userId" value="@(context.Request.Headers.GetValueOrDefault("Authorization","").Split(' ')[1].AsJwt()?.Subject)" />
    <cache-lookup-value key="@("templates-" + context.Variables["userId"])" variable-name="templates" caching-type="external" />
    <choose>
        <when condition="@(!context.Variables.ContainsKey("templates"))">
            <send-request mode="new" response-variable-name="templateResponse" timeout="15" ignore-error="true">
                <set-url>https://appname.azurewebsites.net/api/templates</set-url>
                <set-method>GET</set-method>
                <set-header name="Authorization" exists-action="override">
                    <value>@(context.Request.Headers.GetValueOrDefault("Authorization",""))</value>
                </set-header>
            </send-request>
            <set-variable name="templates" value="@(((IResponse)context.Variables["templateResponse"]).Body.As<string>())" />
            <cache-store-value key="@("templates-" + context.Variables["userId"])" value="@((string)context.Variables["templates"])" duration="10000" />
        </when>
    </choose>
    <base />
</inbound>
<backend>
    <base />
</backend>
<outbound>
    <set-body>@((string)context.Variables["templates"])</set-body>
    <base />
</outbound>
<on-error>
    <base />
</on-error>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM