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
2
-
Are you saying that you have a Redis cluster made in Elasticache and a separate EC2 instance with PHP running on it that you would like to access that Elasticache cluster? What does your PHPRedis connect statement look like?Ivan– Ivan2017-08-14 23:51:08 +00:00Commented Aug 14, 2017 at 23:51
-
1Thanks for replying and yes, but same vpc. i used phpredis. It is able to connect to redis on local but not elasticache although i'm able to access elasticache using command line in the same ec2 instance. $redis = new Redis(); $redis->pconnect('{endpoint of redis elasticache}',6379);Prem Reddy– Prem Reddy2017-08-15 10:17:31 +00:00Commented Aug 15, 2017 at 10:17
Add a comment
|
1 Answer
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. }
1 Comment
Anoop P S
Hi frank i have checked this code , but when we give error endpoint it doesnt gives Exception , still runs the get command any idea