Sunday, August 14, 2016

Integrate Redis on Laravel 5.2

In this tutorial i am going to write basic steps to integrate Redis with laravel 5.2 

Steps

First

Install Redis server on your system.

I am using  Ubuntu 14.04. Following commands will setup Redis Server on your machine.



sudo add-apt-repository ppa:chris-lea/redis-server

sudo apt-get update

sudo apt-get install redis-server
Now start Redis server using following command
sudo service redis-server start
If everything is okay, you'll see this message on Terminal
Starting redis-server: redis-server
For windows user, you can find details about installation in here After successfully installing Redis server on your system, now we have to integrate it with Laravel 5.2 project.
To do that, we need to install predis/predis package  via composer.
Go to your project directory and from terminal run this command
composer require predis/predis
After installing predis/predis package via composer, we have to configure it.
Now open .env file in a text editor and add following line at the buttom.

#CACHE_DRIVER=redis
#SESSION_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
You can change it depending on your system's environment.
Now our integration is done and it's time to test it.

To test it, go to routes.php file and add following line
use Illuminate\Support\Facades\Redis;
Now add following line
Route::get('redis/test',function(){
    $redis = Redis::connection();    
$views=$redis->incr('view');
    dd($views);});
Now run following command on terminal:
sudo service redis-server start

php artisan serve
After starting project, open your browser and browse
localhost:8000/redis/test
Now you'll notice , every time you browse this url, value showing on browser always incremented by one.

0 comments:

Post a Comment