Laravel & Redis

Laravel and Redis are both great pieces of technology at the forefront of technical innovation. This site uses both these great technologies to provide a superior user experience.


Laravel is a PHP framework that unlike the older PHP frameworks dropped support for any legacy, greatly enhancing what it is capable of. It is an opinionated view of PHP that brings together some of the best practices developing with PHP. It has some other great features usually found in other languages like Dependency Injection and a queuing system.


Redis is an in memory persisted key-value data structure server with some really neat feature what make it a perfect fit somewhere in just about any architecture. The common use cases for Redis includes: caching, session management, NoSQL storage, pub/sub events. There is a great deal of hype around Redis that transcends language boundaries and software disciplines, and that should tell you something.

Working together

The great news is that Laravel comes with really good support for Redis. It can support caching and session management out of the box and use Redis as a NoSQL store without any major configuration.

By default Laravel tries to use the localhost Redis installation without a username or password making it really easy to setup on a development environment.

Adding PRedis to composer.json

"require": {
     "illuminate/redis": "5.0.26",
     "laravel/framework": "5.0.26",
     "facebook/php-sdk-v4" : "4.0.23",
     "nesbot/carbon": "1.18.0",
     "predis/predis": "1.0.1"

In the config/database.php file

'redis' => [
    'cluster' => false,
    'default' => [
        'host' => '',
        'port' => 6379,
        'database' => 0,

Setting up the redis as the infrastructure provider


Accessing Redis as a NoSQL store

$redis = Redis::connection();
$redis->set('key', 'value');
$value = $redis->get('key');
$values = $redis->lrange('lkey', 5, 10);

using the sytnax $redis->”function”() you can call any function defined in the Redis command list

Tracking Page Views on this site

public function article($slug)
    $article = CmsArticle::where('slug', $slug)->where(array('status_id' => 2))->first();
    $prevArticle = CmsArticle::where('created_at', '<', $article->created_at)->orderBy('created_at', 'desc')->first();
    $comments = CmsComment::where(array('article_id' => $article->id, 'published' => 1))->orderBy('created_at', 'desc')->get();

    $redis = Redis::connection();
    $redis->incr('' . $slug);

    return view('article/article')->with(array(
        'article' => $article,
        'prevArticle' => $prevArticle,
        'comments' => $comments