

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Java 向けの ElastiCache クラスタークライアントの使用
<a name="AutoDiscovery.Using.ModifyApp.Java"></a>

以下のプログラムは、ElastiCache クラスタークライアントを使用してクラスター設定エンドポイントに接続し、キャッシュにデータ項目を追加する方法を示しています。さらに操作を行わなくても、プログラムは自動検出を使用してクラスター内のすべてのノードに接続します。

```
package com.amazon.elasticache;

import java.io.IOException;
import java.net.InetSocketAddress;

// Import the &AWS;-provided library with Auto Discovery support 
import net.spy.memcached.MemcachedClient;  

public class AutoDiscoveryDemo {

    public static void main(String[] args) throws IOException {
            
        String configEndpoint = "mycluster.fnjyzo.cfg.use1.cache.amazonaws.com";
        Integer clusterPort = 11211;

        MemcachedClient client = new MemcachedClient(
                                 new InetSocketAddress(configEndpoint, 
                                                       clusterPort));       
        // The client will connect to the other cache nodes automatically.

        // Store a data item for an hour.  
        // The client will decide which cache host will store this item. 
        client.set("theKey", 3600, "This is the data value");
    }
}
```

ElastiCache サーバーレスキャッシュでは、常に転送時の暗号化 (TLS) が有効になっています。ノードベースのクラスターで TLS を有効にすることもできます。TLS 対応キャッシュに接続するには、次の例に示すように、SSLContext を使用してクライアントを設定します。

**注記**  
`setSSLContext` メソッドは、spymemcachedクライアントの AWS フォークでのみ使用できます。ソースについては、 GitHubウェブサイトの[aws-elasticache-cluster-client-memcached-for-java](https://github.com/awslabs/aws-elasticache-cluster-client-memcached-for-java)「」を参照してください。このメソッドは、標準のオープンソースspymemcachedライブラリの一部ではありません。

```
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.ConnectionFactoryBuilder;
import net.spy.memcached.MemcachedClient;
public class TLSDemo {
    public static void main(String[] args) throws Exception {
        ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder();
        // Build SSLContext
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init((KeyStore) null);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);
        // Create the client in TLS mode
        connectionFactoryBuilder.setSSLContext(sslContext);
        MemcachedClient client = new MemcachedClient(connectionFactoryBuilder.build(), AddrUtil.getAddresses("mycluster.fnjyzo.cfg.use1.cache.amazonaws.com:11211"));

        // Store a data item for an hour.
        client.set("theKey", 3600, "This is the data value");
    }
}
```

TLS との接続の詳細については、「」を参照してください[Java を使用して TLS Memcached クライアントを作成する](in-transit-encryption.md#in-transit-encryption-connect-java)。サーバーレスキャッシュの場合は、ポート 11211 でキャッシュのサーバーレスエンドポイントを使用します。サーバーレスエンドポイントのアドレスに「.cfg」が含まれていません。TLS が有効になっているノードベースのクラスターの場合は、「.cfg」を含む設定エンドポイントを使用します。