开发者

Connecting to Redis To Go with PHP

I am a newbie with Redis...I just recently picked up Redisent to work with Redis in PHP...and I am having tons of fun! However, I signed up for the Redis to go service, and have been beating my head to connect to use the service...

The URI string is as follows:

redis://[username]:[pass]@[server].redistogo.com:[port]/

The Redisent client simply takes in the hostname and the port...and there's no place for me to enter the username/password... :-/ I've been fiddling around with the fsockopen() function, too...but no score.

Has anyone tried connecting to Redis to go with PHP? If so, any insights or pointers wou开发者_开发百科ld be greatly appreciated!

Note: I realize that there is a REST API available, but that's for provisioning instances, not for the actual operations such as GET/SET,etc.


$db = 1;
$password='password';
$aRedisServers['host']="127.0.0.1";
$aRedisServers['port']= "6379";
$r = new Predis_Client();
$r->connect($aRedisServers['host'], $aRedisServers['port']);
$r->auth($password);
$r->select($db);

$r->set("set","new");
echo $r->get("set");
//output new


predis is the prefered library(active development => 6 Januari 2011) to use.

redis://$x:$y@$z

Then you need the following code to get it working(I tested it):

<?php

require('./Predis.php');

#redis://$x:$y@$z
$redis   = new Predis\Client('redis://$z');
$redis->auth($y);
$redis->incr('counter');
echo $redis->get('counter');
echo "\n";

The strange thing is $x. It is not needed at all?


According to the documentation now, you can do this as part of the instantiation...

$redis = new Predis\Client(array(
    'host'     => '10.0.0.1', 
    'password' => 'secret', 
    'database' => 10, 
));

or

$redis = new Predis\Client('redis://10.0.0.1/?password=secret&database=10');


You can use php-redis to authenticate using ACL (username and password combo)

Following example works for me using AWS MemoryDB for redis.

$redis = new \Redis();
$redis->connect('tsl://ENDPOINT', 6379);
$redis->rawCommand("auth", "USERNAME", "PASSWORD");
echo $redis->set("test","2");
echo $redis->get("test");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜