Using Redis as cache in Laravel

Fri, Oct 14, 2016

#Laravel #cache #redis #performance #PHP #composer

We use cache in Laravel to speed up our application, improving the performance by not having the application to hit the database or call external API everytime the page is loaded. In this post, we will see how to configure and use Redis as the cache layer of the Laravel application.

  1. First of all, you will need to install Redis on our local machine if you don’t have it running yet. Download the distribution from Redis homepage and install the software following the installation instruction.
  2. Download composer and install it.
  3. Launch a terminal and type the following command to create a new project
    $ composer create-project --prefer-dist laravel/laravel redis-sample
    
    Installing laravel/laravel (v5.2.31)
      - Installing laravel/laravel (v5.2.31)
        Downloading: 100%         
    
    Created project in redis-sample
    > php -r "copy('.env.example', '.env');"
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
      - Installing vlucas/phpdotenv (v2.4.0)
        Loading from cache
    
      - Installing symfony/polyfill-mbstring (v1.2.0)
        Downloading: 100%         
    
      - Installing symfony/var-dumper (v3.0.9)
        Loading from cache
    
      - Installing symfony/translation (v3.0.9)
        Loading from cache
    
      - Installing symfony/routing (v3.0.9)
        Loading from cache
    
      - Installing symfony/process (v3.0.9)
        Loading from cache
    
      - Installing symfony/polyfill-util (v1.2.0)
        Downloading: 100%         
    
      - Installing symfony/polyfill-php56 (v1.2.0)
        Downloading: 100%         
    
      - Installing psr/log (1.0.2)
        Loading from cache
    
      - Installing symfony/debug (v3.0.9)
        Loading from cache
    
      - Installing symfony/http-foundation (v3.0.9)
        Loading from cache
    
      - Installing symfony/event-dispatcher (v3.1.5)
        Loading from cache
    
      - Installing symfony/http-kernel (v3.0.9)
        Loading from cache
    
      - Installing symfony/finder (v3.0.9)
        Loading from cache
    
      - Installing symfony/console (v3.0.9)
        Loading from cache
    
      - Installing swiftmailer/swiftmailer (v5.4.3)
        Downloading: 100%         
    
      - Installing jakub-onderka/php-console-color (0.1)
        Downloading: 100%         
    
      - Installing jakub-onderka/php-console-highlighter (v0.3.2)
        Downloading: 100%         
    
      - Installing dnoegel/php-xdg-base-dir (0.1)
        Downloading: 100%         
    
      - Installing nikic/php-parser (v2.1.1)
        Loading from cache
    
      - Installing psy/psysh (v0.7.2)
        Downloading: 100%         
    
      - Installing paragonie/random_compat (v1.4.1)
        Loading from cache
    
      - Installing nesbot/carbon (1.21.0)
        Downloading: 100%         
    
      - Installing mtdowling/cron-expression (v1.1.0)
        Downloading: 100%         
    
      - Installing monolog/monolog (1.21.0)
        Downloading: 100%         
    
      - Installing league/flysystem (1.0.32)
        Downloading: 100%         
    
      - Installing jeremeamia/superclosure (2.2.0)
        Downloading: 100%         
    
      - Installing doctrine/inflector (v1.1.0)
        Downloading: 100%         
    
      - Installing classpreloader/classpreloader (3.0.0)
        Downloading: 100%         
    
      - Installing laravel/framework (v5.2.45)
        Loading from cache
    
      - Installing fzaninotto/faker (v1.6.0)
        Downloading: 100%         
    
      - Installing hamcrest/hamcrest-php (v1.2.2)
        Downloading: 100%         
    
      - Installing mockery/mockery (0.9.5)
        Downloading: 100%         
    
      - Installing symfony/yaml (v3.1.5)
        Loading from cache
    
      - Installing sebastian/version (1.0.6)
        Downloading: 100%         
    
      - Installing sebastian/global-state (1.1.1)
        Downloading: 100%         
    
      - Installing sebastian/recursion-context (1.0.2)
        Downloading: 100%         
    
      - Installing sebastian/exporter (1.2.2)
        Downloading: 100%         
    
      - Installing sebastian/environment (1.3.8)
        Downloading: 100%         
    
      - Installing sebastian/diff (1.4.1)
        Downloading: 100%         
    
      - Installing sebastian/comparator (1.2.0)
        Downloading: 100%         
    
      - Installing doctrine/instantiator (1.0.5)
        Downloading: 100%         
    
      - Installing phpunit/php-text-template (1.2.1)
        Downloading: 100%         
    
      - Installing phpunit/phpunit-mock-objects (2.3.8)
        Downloading: 100%         
    
      - Installing phpunit/php-timer (1.0.8)
        Downloading: 100%         
    
      - Installing phpunit/php-file-iterator (1.4.1)
        Downloading: 100%         
    
      - Installing phpunit/php-token-stream (1.4.8)
        Downloading: 100%         
    
      - Installing phpunit/php-code-coverage (2.2.4)
        Downloading: 100%         
    
      - Installing webmozart/assert (1.1.0)
        Downloading: 100%         
    
      - Installing phpdocumentor/reflection-common (1.0)
        Downloading: 100%         
    
      - Installing phpdocumentor/type-resolver (0.2)
        Downloading: 100%         
    
      - Installing phpdocumentor/reflection-docblock (3.1.1)
        Loading from cache
    
      - Installing phpspec/prophecy (v1.6.1)
        Downloading: 100%         
    
      - Installing phpunit/phpunit (4.8.27)
        Downloading: 100%         
    
      - Installing symfony/css-selector (v3.0.9)
        Downloading: 100%         
    
      - Installing symfony/dom-crawler (v3.0.9)
        Downloading: 100%         
    sebastian/global-state suggests installing ext-uopz (*)
    phpunit/php-code-coverage suggests installing ext-xdebug (>=2.2.1)
    phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
    Writing lock file
    Generating autoload files
    > Illuminate\Foundation\ComposerScripts::postUpdate
    > php artisan optimize
    Generating optimized class loader
    > php artisan key:generate
    Application key [base64:DFe7QYYsF2VSc7h/ZkmPJ3xNcweIL+1LbEcWkxIlVPs=] set successfully.
    
    Okay, it’s a lengthy output after create the project, but now we have a new Laravel project ‘redis-sample’
  4. Now we cd into ‘redis-sample’ directory and make a new .env file
    $ php -r "copy('.env.example', '.env');"
    
  5. Now we install the Redis library for Laravel using composer
    $ composer require predis/predis
    Using version ^1.1 for predis/predis
    ./composer.json has been updated
    Loading composer repositories with package information
    Updating dependencies (including require-dev)
      - Installing predis/predis (v1.1.1)
        Loading from cache
    
    predis/predis suggests installing ext-phpiredis (Allows faster serialization and deserialization of the Redis protocol)
    Writing lock file
    Generating autoload files
    > Illuminate\Foundation\ComposerScripts::postUpdate
    > php artisan optimize
    Generating optimized class loader
    
    Okay we are good.
  6. Now we open and edit the .env file we’ve created at step 3
    APP_ENV=local
    APP_DEBUG=true
    APP_KEY=SomeRandomString
    APP_URL=http://localhost
    
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=homestead
    DB_USERNAME=homestead
    DB_PASSWORD=secret
    
    CACHE_DRIVER=redis
    SESSION_DRIVER=file
    QUEUE_DRIVER=sync
    
    REDIS_HOST=127.0.0.1
    REDIS_PASSWORD=null
    REDIS_PORT=6379
    
    MAIL_DRIVER=smtp
    MAIL_HOST=mailtrap.io
    MAIL_PORT=2525
    MAIL_USERNAME=null
    MAIL_PASSWORD=null
    MAIL_ENCRYPTION=null
    
    All we need to do is change the value of CACHE_DRIVER from “file” to “redis”. You might want to change the REDIS_HOST or REDIS related configurations if you have a different configuration
  7. a sample controller to get value from cache
  8. verify cache is working using redis cli
  9. remove cache