本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
以下程序演示了如何使用 ElastiCache 集群客户端连接到集群配置终端节点并将数据项添加到缓存。借助 Auto Discovery,程序可在没有任何进一步干预的情况下连接至集群中的所有节点。
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");
}
}