Cannot connect Redis Cluster in Elasticache to PHP using phpredis library

三世轮回 提交于 2019-12-25 08:49:45

问题


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:


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


来源:https://stackoverflow.com/questions/45684076/cannot-connect-redis-cluster-in-elasticache-to-php-using-phpredis-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!