无法使用phpredis库将Elasticache中的Redis Cluster连接到PHP

时间:2017-08-14 22:42:05

标签: php redis amazon-elasticache

我可以使用ec2实例(在aws文档中给出)连接到elasticache中的redis集群,并且能够添加和获取键,值。但是,当我尝试通过相同的ec2实例上的phpredis连接时,我没有得到任何错误,也没有数据。请帮我解决一下这个。对于这个特定问题,互联网上的信息不多。我可以连接到在同一个ec2实例上运行的redis,但不能连接到elasticache。如果我能得到一些关于如何改变主机(redis集群的端点)的例子。 感谢

1 个答案:

答案 0 :(得分:0)

  1. 使用Predis库。

  2. 使用Predis以群集模式连接到Redis ElastiCache端点,请参见以下示例。

    try{ 
        // Put your AWS ElastiCache Configuration Endpoint here.
        $servers  = ['aliceredis.8xyzwu.clustercfg.euw2.cache.amazonaws.com:6379'];
        // Tell client to use 'cluster' mode.
        $options  = ['cluster' => 'redis'];
        // Create your redis client
        $redis = new Predis\Client($servers, $options); 
    
        // Do something you want:
        // Set the expiration for 7 seconds
        $redis->set("tm", "I have data for 7s.");
        $redis->expire("tm", 7);
        $ttl = $redis->ttl("tm"); // will be 7 seconds
    
        // Print out value of the key 'tm'
        var_dump(array("msg"=>"Successfully connected to Redis Cluster.", "val"=>$redis->get("tm"))) ;
    
    }
    catch(Exception $ex){ 
        echo ('Error: ' . $ex->getMessage() ); // output error message.
    }