微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Azure API 管理配置外部缓存策略

我们使用 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>

解决方法

只要请求带有授权标头,APIM 就不会缓存请求,除非您启用私有响应缓存。

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

如果它不能继续工作,那么我建议向 MS 团队提交支持票。

,

我在编写策略时犯了一些错误。这是更正后的政策。

File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/db/backends/sqlite3/creation.py",line 12,in is_in_memory_db
return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'PosixPath' is not iterable.
container_command 01_migrate in .ebextensions/db-migrate.config failed. For more detail,check /var/log/eb-activity.log using console or EB CLI.
2021-06-22 15:34:35    INFO    Command execution completed on all instances. Summary: [Successful: 0,Failed: 1].
2021-06-22 15:34:35    ERROR   Unsuccessful command execution on instance id(s) 'i-0c5bbcb2318daa82b'. Aborting the operation.
2021-06-22 15:34:35    ERROR   Failed to deploy application.
ERROR: ServiceError - Failed to deploy application.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。