Install Redis with PHP

1) install redis server

sudo apt-get update
sudo apt-get install redis-server

2) Make redis use unix socket. modify /etc/redis/redis.conf. Uncomment these lines

unixsocket /var/run/redis/redis.sock
unixsocketperm 777

3) restart redis

sudo /etc/init.d/redis-server restart

2) Install php-dev

sudo apt-get install php-dev
or
sudo apt-get install php5-dev

3) install from pecl

sudo pecl install redis

4) Add extension to php.ini. Everything in conf.d will be read from php.ini

cd /etc/php5/conf.d
vim /etc/php5/conf.d/redis.ini

Add this to file: extension=redis.so

5) restart php and nginx

/etc/init.d/php5-fpm restart
/etc/init.d/nginx restart

6) Connecting to Redis in php

$redis=new Redis();
$redis->connect(‘/var/run/redis/redis.sock’);

7) logic


if(($verifyCheck=$redis->get('somekey'))===FALSE) {
    $verifyCheck = 'someval'.mt_rand();
    // Expire is in secs
    $redis->setex('somekey',10,$verifyCheck);
    echo 'not cache' . "\n";
} else {
    echo 'from cache' . "\n";
}
echo $verifyCheck . "\n"

Leave a comment

0 Comments.