How to use redis as session caching in laravel?

by mallory_cormier , in category: PHP Frameworks , 2 months ago

How to use redis as session caching in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , 2 months ago

@mallory_cormier 

To use Redis as session caching in Laravel, follow these steps:

  1. Install the predis/predis package using Composer:
1
composer require predis/predis


  1. Open your .env file and update the SESSION_DRIVER setting to use Redis:
1
SESSION_DRIVER=redis


  1. Update your config/session.php file to specify Redis as the default session driver:
1
'driver' => env('SESSION_DRIVER', 'redis'),


  1. Update your config/database.php file to include the Redis connection details:
1
2
3
4
5
6
7
8
9
'redis' => [
    'client' => 'predis',
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],
],


  1. Update your config/cache.php file to specify Redis as the default cache driver:
1
'default' => env('CACHE_DRIVER', 'redis'),


  1. Update your .env file to include the Redis connection details for caching:
1
2
3
4
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379


  1. Update your app/Http/Kernel.php file to use Redis as the session driver and cache store:
1
2
3
4
5
6
7
use IlluminateSessionMiddlewareStartSession;

protected $middleware = [
    // Other middleware entries...

    StartSession::class,
];


Now, your Laravel application is configured to use Redis as the session caching. Make sure you have Redis installed and running on your server for this to work properly.