使用适用于 PHP 的 ElastiCache Cluster Client - Amazon ElastiCache
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用适用于 PHP 的 ElastiCache Cluster Client

下述程序演示了如何使用 ElastiCache Cluster Client 以连接到集群配置端点,并将数据项目添加至缓存中。借助 Auto Discovery,程序将在没有任何进一步干预的情况下连接至集群中的所有节点。

如要使用适用于 PHP 的 ElastiCache Cluster Client,您首先需要将其安装在您的 Amazon EC2 实例上。有关更多信息,请参阅安装适用于 PHP 的 ElastiCache Cluster Client

<?php /** * Sample PHP code to show how to integrate with the Amazon ElastiCache * Auto Discovery feature. */ /* Configuration endpoint to use to initialize memcached client. * This is only an example. */ $server_endpoint = "mycluster.fnjyzo.cfg.use1.cache.amazonaws.com"; /* Port for connecting to the ElastiCache cluster. * This is only an example */ $server_port = 11211; /** * The following will initialize a Memcached client to utilize the Auto Discovery feature. * * By configuring the client with the Dynamic client mode with single endpoint, the * client will periodically use the configuration endpoint to retrieve the current cache * cluster configuration. This allows scaling the cache cluster up or down in number of nodes * without requiring any changes to the PHP application. * * By default the Memcached instances are destroyed at the end of the request. * To create an instance that persists between requests, * use persistent_id to specify a unique ID for the instance. * All instances created with the same persistent_id will share the same connection. * See http://php.net/manual/en/memcached.construct.php for more information. */ $dynamic_client = new Memcached('persistent-id'); $dynamic_client->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE); $dynamic_client->addServer($server_endpoint, $server_port); /** * Store the data for 60 seconds in the cluster. * The client will decide which cache host will store this item. */ $dynamic_client->set('key', 'value', 60); /** * Configuring the client with Static client mode disables the usage of Auto Discovery * and the client operates as it did before the introduction of Auto Discovery. * The user can then add a list of server endpoints. */ $static_client = new Memcached('persistent-id'); $static_client->setOption(Memcached::OPT_CLIENT_MODE, Memcached::STATIC_CLIENT_MODE); $static_client->addServer($server_endpoint, $server_port); /** * Store the data without expiration. * The client will decide which cache host will store this item. */ $static_client->set('key', 'value'); ?>

有关如何在启用 TLS 的情况下使用 ElastiCache 集群客户端的示例,请参阅对 PHP 和 Memcached 使用传输中加密