Howto: Handle phpredis connection error
Example Code:
<?php $redis=new Redis(); $connected= $redis->connect('127.0.0.1', 6379); if(!$connected) { // some other code to handle connection problem die( "Cannot connect to redis server.\n" ); } $redis->setex('somekey', 60, 'some value');
I came up with the above code after reading the doc because I found out the following try/catch code doesn’t work as expected
<?php $redis=new Redis(); try { $redis->connect('127.0.0.1', 6379); } catch (Exception $e) { // tried changing to RedisException, didn't work either die( "Cannot connect to redis server:".$e->getMessage() ); } $redis->setex('somekey', 60, 'some value');
Install phpredis on Ubuntu
Tested on Ubuntu 10.10 64bit server edition but should apply to other versions as well.
1) Preparation
sudo apt-get install php5-dev
php5-dev provides the dev library as well as the phpize command which is required for the compiling step
2) Get phpredis source code, should be pretty easy by running
git clone git://github.com/nicolasff/phpredis.git
3) Compile and install
cd phpredis phpize ./configure make sudo -s make install
4) Enable the phpredis extension
sudo -s echo "extension=redis.so">/etc/php5/conf.d/redis.ini exit
5) Write a simple php script to test (running on cli would be fine if php5-cli is installed)
<?php // phpredis_set.php $redis=new Redis() or die("Can'f load redis module."); $redis->connect('127.0.0.1'); $redis->set('set_testkey', 1);
Prior to try phpredis I was using Rediska as the php redis client. I did some pretty quick and dirty benchmarking comparison and phpredis is clearly a winner here, not surprisingly because phpredis is a compiled extension written in C while Rediska is a pure php library.
time for i in `seq 1 1000`; do php phpredis_set.php; done
real 0m13.072s
user 0m6.560s
sys 0m3.620s
time for i in `seq 1 1000`; do php rediska_set.php; done
real 0m21.035s
user 0m12.150s
sys 0m5.050s
and the source code for rediska_set.php:
<?php require_once 'Rediska/library/Rediska.php'; $rediska=new Rediska(); $rediska->set('set_testkey', 1);
The above tests were conducted on a single-core i3 2.1GHZ Virtualbox guest with 512MB of RAM.
Example: sorted set functions with node.js + redis
Have been playing with node.js module redis (and a bit of underscore) almost all day and I could not find any examples using zrange (or zrevrange) so I decided to write one as a practice:
var rc=require('redis').createClient(); var _=require('underscore'); rc.zincrby('myset', 1, 'usera'); rc.zincrby('myset', 5, 'userb'); rc.zincrby('myset', 3, 'userc'); rc.zrevrange('myset', 0, -1, 'withscores', function(err, members) { // the resulting members would be something like // ['userb', '5', 'userc', '3', 'usera', '1'] // use the following trick to convert to // [ [ 'userb', '5' ], [ 'userc', '3' ], [ 'usera', '1' ] ] // learned the trick from // http://stackoverflow.com/questions/8566667/split-javascript-array-in-chunks-using-underscore-js var lists=_.groupBy(members, function(a,b) { return Math.floor(b/2); }); console.log( _.toArray(lists) ); }); rc.quit();
Have fun.
Getting last N element(s) from a list in redis
Somehow I couldn’t find anywhere on how to achieve this: getting the last N elements from a list in redis, not even on Redis command docs. Well I just figured out the easiest way, say to get last 3 elements from list users:
redis 127.0.0.1:6379> rpush users tom (integer) 1 redis 127.0.0.1:6379> rpush users ray (integer) 2 redis 127.0.0.1:6379> rpush users coolguy (integer) 3 redis 127.0.0.1:6379> rpush users bob (integer) 4 redis 127.0.0.1:6379> rpush users alice (integer) 5 redis 127.0.0.1:6379> lrange users -3 -1 1) "coolguy" 2) "bob" 3) "alice" redis 127.0.0.1:6379>
Just a couple of things worth noting:
- start should always to be no larger than end in command lrange list_name start end
- so the formula to get last N elements from a list is lrange list_name -N -1 where N>=1