キャッシュへの格納

適用対象: すべての API Management レベル

cache-storeポリシーは、指定されたキャッシュ設定に従って応答をキャッシュに格納します。 このポリシーを適用できるのは、応答の内容が一定期間にわたって静的である場合です。 応答のキャッシュを使用すると、バックエンド Web サーバーの帯域幅および処理の要件が低減され、API コンシューマーによって認識される遅延が小さくなります。

Note

このポリシーには、対応するキャッシュから取得ポリシーが必要です。

重要

組み込みのキャッシュは揮発性であり、同じ API Management サービスの同じリージョン内のすべてのユニットによって共有されます。

Note

ポリシーの要素と子要素を、ポリシー ステートメントで指定された順序で設定します。 このポリシーの構成に役立つ、ガイド付きのフォーム ベース エディターがポータルに用意されています。 API Management ポリシーを設定または編集する方法について説明します。

ポリシー ステートメント

<cache-store duration="seconds" cache-response="true | false" />

属性

属性 説明 必要 Default
duration キャッシュに格納されたエントリの有効期間 (秒単位)。 ポリシー式を使用できます。 はい 該当なし
cache-response 現在の HTTP 応答をキャッシュする場合は true に設定します。 属性が省略されている場合は、状態コード 200 OK を含む HTTP 応答のみがキャッシュされます。 ポリシー式を使用できます。 いいえ false

使用法

使用上の注意

  • API Management では、HTTP GET 要求への応答のみがキャッシュされます。
  • このポリシーは、ポリシー セクションで 1 回だけ使用できます。

対応する cache-lookup ポリシーの例

<policies>
    <inbound>
        <base />
        <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" downstream-caching-type="none" must-revalidate="true" caching-type="internal" >
            <vary-by-query-parameter>version</vary-by-query-parameter>
        </cache-lookup>
    </inbound>
    <outbound>
        <cache-store duration="seconds" />
        <base />
    </outbound>
</policies>

ポリシー式の使用例

この例は、API Management 応答のキャッシュ時間を、バックエンド サービスの Cache-Control ディレクティブによって指定されたバックエンド サービスの応答キャッシュ時間と一致するように構成する方法を示しています。

<!-- The following cache policy snippets demonstrate how to control API Management response cache duration with Cache-Control headers sent by the backend service. -->

<!-- Copy this snippet into the inbound section -->
<cache-lookup vary-by-developer="false" vary-by-developer-groups="false" downstream-caching-type="public" must-revalidate="true" >
  <vary-by-header>Accept</vary-by-header>
  <vary-by-header>Accept-Charset</vary-by-header>
</cache-lookup>

<!-- Copy this snippet into the outbound section. Note that cache duration is set to the max-age value provided in the Cache-Control header received from the backend service or to the default value of 5 min if none is found  -->
<cache-store duration="@{
    var header = context.Response.Headers.GetValueOrDefault("Cache-Control","");
    var maxAge = Regex.Match(header, @"max-age=(?<maxAge>\d+)").Groups["maxAge"]?.Value;
    return (!string.IsNullOrEmpty(maxAge))?int.Parse(maxAge):300;
  }"
 />

詳細については、ポリシー式およびコンテキスト変数に関する各ページを参照してください。

ポリシーに対する処理の詳細については、次のトピックを参照してください。