问题
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:
Use Predis library.
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. }
来源:https://stackoverflow.com/questions/45684076/cannot-connect-redis-cluster-in-elasticache-to-php-using-phpredis-library