---
schema_version: "1"
id: f93296fca49d75457e955d33cd79bf5fcbc7df60663285ecee590c893527a8d0
title: Mule 4 のカスタムポリシーのキャッシュ
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/mule-gateway/policies-custom-mule-4-caching.md
canonical_url: https://docs.mulesoft.com/jp/mule-gateway/policies-custom-mule-4-caching
source_site: docs.mulesoft.com
content_type: documentation
content_hash: b45d1083c2ec09d198eeba91bc2c47d51ca64d2a9a2f16639ea4ed5ace484a7d
description: MuleSoft Documentation Site
latest: true
---
# Mule 4 のカスタムポリシーのキャッシュ

外部システムからトークンなどの値を取得してアプリケーションまたはポリシーで使用する必要がある場合、値を ​[Mule 4 Object Store](https://docs.mulesoft.com/object-store-connector/latest/)​ に一時的に保存できます。​ [Object Store Connector に関する情報は Anypoint Exchange を参照](https://www.anypoint.mulesoft.com/exchange/org.mule.connectors/mule-objectstore-connector/)​してください。​[Object Store バックエンドドキュメント](https://docs.mulesoft.com/jp/object-store/)​も参照してください。

Object Store へのアクセスは、\[Anypoint Platform\] > \[Runtime Manager\] から有効にすることができます。ただし、Runtime Manager Object Store は ​[CloudHub](https://docs.mulesoft.com/cloudhub/)​ およびハイブリッドアプリケーションのみを対象とします。

オブジェクトストアにデータを保存するアプリケーション、またはそのいずれかのワーカーがデータを取得できます。オブジェクトストアではカスタムポリシーのキャッシュが提供されるため、外部コールを何度も実行することが回避されます。また、CloudHub 内のさまざまなインスタンスで実行されているさまざまな API に適用されている複数のポリシーでオブジェクトストアを使用できます。

## [](#オブジェクトストアを使用したキャッシュ)オブジェクトストアを使用したキャッシュ

Mule は、値を取得して最大 30 日間保存する Mule 4 Object Store Connector を提供します。

次の例では、データをオブジェクトストアから取得します。特定のキーのデータが見つからない場合は、外部システムから新しい値を取得してオブジェクトストアに保存します。

```xml
<try>
   <os:retrieve key="token" target="myVar" objectStore="objectStore"/>

   <error-handler>
       <on-error-continue type="OS:KEY_NOT_FOUND" logException="false">
           <http:request method="GET"
            url="http://localhost:8081/retrieve"
            target="myVar"/>

           <os:store key="token" objectStore="objectStore">
               <os:value>
                   #[vars.myVar]
               </os:value>
           </os:store>
       </on-error-continue>
   </error-handler>
</try>
```

この例の ​`<os:retrieve key="token" target="myVar" objectStore="objectStore"/>`​ ステートメントでは、値トークンを検索のキーとして使用して、Object Store Connector の取得操作を実行し、取得した値を ​`myVar`​ 変数に割り当てます。

キーに関連付けられた値がない場合、取得操作によりエラーがスローされます。このため、操作は ​`<try>`​ ブロックで囲まれています。これにより、エラーが発生した場合、最新の値を取得してオブジェクトストアに保存することでエラーを処理できます。

HTTP 要求は ​`<error-handler>`​ ブロック内で実装されています。値は API コールを介して取得され、​`myVar`​ 変数に割り当てられます。次に、Object Store Connector の ​`store`​ 操作を使用して、値はオブジェクトストアに保存されます。

このブロックが実行されると、キャッシュまたは外部システムから取得された値が myVar に含まれます。この値は、キャッシュに保存され、後で使用されます。この値を任意の目的で使用できます。たとえば、この値を新しいヘッダーとして要求や応答などに追加できます。

## [](#オブジェクトストアの定義)オブジェクトストアの定義

Object Store Connector の操作を使用するには、まずその操作で使用するオブジェクトストアを定義する必要があります。

次の設定では、myObjectStore を定義し、値を 1 時間保存します。この例のオブジェクトストアは永続的として設定されているため、ランタイムが再起動しても Object Store はクリアされません。

```xml
<os:object-store name="myObjectStore" persistent="true"
 entryTtl="1" entryTtlUnit="HOURS"/>
```

存続期間 (TTL) の値は 1 時間に設定されています。

## [](#runtime-manager-でのキャッシュ)Runtime Manager でのキャッシュ

Runtime Manager で実行されている API にポリシーをデプロイし、そのインスタンスでも Object Store v2 (OSv2) が有効になっている場合、上記の永続的オブジェクトストアの設定を使用すると、ポリシーは値を Cloud Object Store にキャッシュします。

ただし、制限があります。Object Store Connector では新しいストアを作成できないため、設定されたエントリ存続期間は使用時に順守されません。代わりに、30 日の TTL を持つデフォルトのオブジェクトストアパーティションが使用されます。

## [](#multiple-api-のオブジェクトストアの有効化)Multiple API のオブジェクトストアの有効化

Object Store v2 を使用している場合、複数の API デプロイ間でキャッシュを共有できます。 これを実現するには、オブジェクトストアの設定に含まれる属性の名前が、デプロイされるすべてのポリシーで同じである必要があるほか、ランタイムには、true の値が設定されている ​`objectstore.enable.global.sharing`​ という名前のシステムプロパティが含まれる必要があります。

これを有効にすると、該当のインスタンスで実行されているポリシーで保持されているデータを、同じ環境下の他のインスタンスで実行されているポリシーでも使用できます。

**警告:**​ グローバル共有の OSv2 モードを有効にすると、アプリケーションまたは他のポリシーで使用されているオブジェクトストアが組織のすべての API でも共有されるようになります。

## [](#オブジェクトストアを使用したポリシーのキャッシュの完全な例の確認)オブジェクトストアを使用したポリシーのキャッシュの完全な例の確認

この例は、外部システムから取得し、Mule Object Store にキャッシュした値を使用して、ヘッダーを受信 HTTP 要求に追加するポリシーを示しています。

```xml
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:os="http://www.mulesoft.org/schema/mule/os"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:http-policy="http://www.mulesoft.org/schema/mule/http-policy"
xmlns:http-transform="http://www.mulesoft.org/schema/mule/http-policy-transform"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/http-policy
http://www.mulesoft.org/schema/mule/http-policy/current/mule-http-policy.xsd
http://www.mulesoft.org/schema/mule/http-policy-transform
http://www.mulesoft.org/schema/mule/http-policy-transform/current/mule-http-policy-transform.xsd
http://www.mulesoft.org/schema/mule/os
http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd">

 <!-- Object Store configuration -->
 <os:object-store name="myObjectStore"
 persistent="true"
 entryTtl="1"
 entryTtlUnit="HOURS"/>

 <http-policy:proxy name="caching">
     <http-policy:source>
         <try>
             <!-- Checking Cache for already stored value -->
             <os:retrieve key="aKey" target="myVar" objectStore="myObjectStore"/>

             <error-handler>
                 <on-error-continue type="OS:KEY_NOT_FOUND" logException="false">
                     <!-- If value not found, then retrieve it from  -->
                     <!-- the external system. -->
                     <http:request method="GET"
                     url="http://localhost:8081/retrieve"
                     target="myVar"/>

                     <!-- After retrieving a fresh value, store it in the cache -->
                     <os:store key="aKey" objectStore="myObjectStore">
                         <os:value>
                             #[vars.myVar]
                         </os:value>
                     </os:store>
                 </on-error-continue>
             </error-handler>
         </try>

         <!-- Use the retrieved value to add a new header -->
         <http-transform:add-headers outputType="request">
             <http-transform:headers>#[{'new-custom-header': vars.myVar}]</http-transform:headers>
         </http-transform:add-headers>

         <http-policy:execute-next/>

     </http-policy:source>
 </http-policy:proxy>

</mule>
```

この例で必要な連動関係は次のようになります。

```xml
<dependencies>
   <dependency>
       <groupId>org.mule.connectors</groupId>
       <artifactId>mule-http-connector</artifactId>
       <version>1.2.1</version>
       <classifier>mule-plugin</classifier>
       <scope>provided</scope>
   </dependency>

   <dependency>
       <groupId>org.mule.connectors</groupId>
       <artifactId>mule-objectstore-connector</artifactId>
       <version>1.1.1</version>
       <classifier>mule-plugin</classifier>
   </dependency>

   <dependency>
      <groupId>com.mulesoft.anypoint</groupId>
      <artifactId>mule-http-policy-transform-extension</artifactId>
      <version>1.1.0</version>
      <classifier>mule-plugin</classifier>
  </dependency>
</dependencies>
```

## [](#関連情報)関連情報

- [Object Store バックエンドドキュメント](https://docs.mulesoft.com/jp/object-store/)