本文介绍了无法使用 phpredis 库将 Elasticache 中的 Redis 集群连接到 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用 ec2 实例(在 aws 文档中给出)连接到 elasticache 中的 redis 集群,并且能够添加和获取键、值.但是当我尝试在同一个 ec2 实例上通过 phpredis 进行连接时,我没有收到错误,也没有数据.请帮我解决一下这个.互联网上没有太多关于这个特定问题的信息.我能够连接到在同一个 ec2 实例上运行的 redis,但不能连接到 elasticache.如果我能得到一些关于除了更改主机(redis 集群的端点)之外如何操作的示例.谢谢

I am able to connect to redis cluster in elasticache with ec2 instance (given in aws documentation) and be able to add and get keys, values. But when i try to connect through phpredis on same ec2 instance, I'm getting no error and also no data. Please Help me with this. There's not much info on the internet for this specific problem. I am able to connect to redis running on the same ec2 instance but not to elasticache. If i could get some example on how to except for changing the host (endpoint of redis cluster).Thanks

推荐答案

  1. 使用 Predis 库.

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

Connect to Redis ElastiCache Endpoint in Cluster mode using Predis, see below example.

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.
}

这篇关于无法使用 phpredis 库将 Elasticache 中的 Redis 集群连接到 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 12:32