Laravel Redis

So far, we've simply used Laravel's Redis facade to publish message (which we then catch from our Node Redis client). This time, let's create a typical event class. As you'll find, as of Laravel 5.1, we only have to implement an interface to trigger broadcasting to the client! The Redis configuration for laravel application is present in the config/database.php. In this file you can see the redis array which contains the Redis servers utilized by laravel application: You can modify this array based on your environment. Each Redis server defined in your configuration file is required to have a name, host, and port.

Introduction

Redis

Laravel provides a unified API for various caching systems. The cache configuration is located in the .env file. In this file you may specify which cache driver you would like used by default throughout your application. Laravel supports popular caching backends like Memcached and Redis out of the box.

Differences From Laravel

The Lumen cache drivers utilize the exact same code as the full Laravel cache drivers. Beyond configuration, there are no differences between using the cache in Lumen and using the cache in Laravel; therefore, please consult the full Laravel documentation for usage examples.

Note: Before using the Cache facade, be sure you have uncommented the $app->withFacades() method call in your bootstrap/app.php file.

Redis Support

Before using a Redis cache with Lumen, you will need to install the illuminate/redis package via Composer. Then, you should register the IlluminateRedisRedisServiceProvider in your bootstrap/app.php file:

If you have not called $app->withEloquent() in your bootstrap/app.php file, then you should call $app->configure('database'); in the bootstrap/app.php file to ensure the Redis database configuration is properly loaded.

Laravel Redis Tutorial

Laravel allows you to use a few different Session drivers.

  • File
  • Memcache
  • Redis
  • Database
  • Cookie
  • Array
Redis

You'll notice in this list is Redis and Memcache that we can also use as our Cache Driver. Redis and Memcache are the fastest options to use for your session driver so you'll find a lot of people using this option.

The problem comes when you're using Redis as both the cache driver and the session driver.

By default these are stored in the same Redis database, therefore if you ever clear the cache of your application you will also remove the session information.

Laravel Redis Eval

Therefore if you have a blog that on publish of a post it will perform a php artisan cache:clear to reset the post data it will also clear all the session data in Redis, which results in all your users being logged out.

Solution

To solve this problem we need to tell Laravel there are different Redis connections, one for Cache and one for Session.

First navigate to config/database.php at the bottom of the file you'll see the settings for redis and there will be a default connection. We need to add a new connection, using the same host, password and host but change the database number.

Laravel Redis

Now we need to tell redis to use this connection as the session. Open up the config/session.php file and find the connection property, then change this to 'session'.

Laravel Redis Ssl

Now when you clear the cache php artisan cache:clear your users will no longer lose their session data and be forcing them to logout.