2

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? Commented Aug 14, 2017 at 23:51
  • 1
    Thanks 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); Commented Aug 15, 2017 at 10:17

1 Answer 1

1
  1. Use Predis library.

  2. 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.
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Hi frank i have checked this code , but when we give error endpoint it doesnt gives Exception , still runs the get command any idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.