Archive

Archive for March, 2012

Howto: Handle phpredis connection error

March 26, 2012 1 comment

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');
Advertisements
Categories: php, redis

Install phpredis on Ubuntu

March 25, 2012 10 comments

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.

Categories: php, phpredis, redis

Sheevaplug + Debian 6 (Squeez) + node.js 0.6.13

March 21, 2012 Leave a comment

1) Preparation

apt-get install build-essential libssl-dev curl
cd /tmp
wget http://nodejs.org/dist/v0.6.13/node-v0.6.13.tar.gz
tar xzf node-v0.6.13.tar.gz
cd node-v0.6.13

2) Edit file deps/v8/SConstruct as follows:

//line #80
  'gcc': {
    'all': {
      'CCFLAGS':      ['$DIALECTFLAGS', '$WARNINGFLAGS', '-march=armv5t', '-mfloat-abi=soft'],
      'CXXFLAGS':     ['-fno-rtti', '-fno-exceptions'],
    },


//line #1081
  'armeabi': {
    'values': ['hard', 'softfp', 'soft'],
    'default': 'soft',
    'help': 'generate calling conventiont according to selected ARM EABI variant'
  },

3) Compile & Install

./configure
make && make install

Step 3) will take about 40 minutes. If installation is successful, command node -v should print v0.6.13.

4) Download and install npm

curl http://npmjs.org/install.sh | sh
npm list -g
ln -s /usr/local/lib/node_modules ~/.node_modules

The ln command is optional but it’ll make require statements much easier.

Categories: Debian, node.js, sheevaplug

Howto: create javascript object without the “new” keyword (quick example)

March 11, 2012 4 comments

[ Update 2015-02-08 ] Please disregard the “technique” I used in this post. I wrote this post almost 4 years ago when I didn’t have a very deep knowledge about javascript. Kudos to reader Howard for pointing out my mistake.

var Apple=function(type, color) {
    this.type=type;
    this.color=color;
    this.getInfo=function() {
        return &amp;quot;This is a &amp;quot;+color+' '+type+' apple.';
    };
    this.grow=function() {
        console.log('See a '+type+' apple needs to grow too.');
    };
    return this;
};

var myapple=Apple('honeycrip', 'red'); // new Apple ... works too
console.log(myapple.getInfo());
myapple.grow();

The new keyword can be omitted due to the use of return this line (10). Happy coding.

Categories: javascript