You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中使用Redis实现会话缓存及额外配置问询

Using Redis as Session Cache in Laravel: Complete Guide

Hey there! Let's break down exactly how to set up Redis for storing user sessions in your Laravel app, including all the extra steps beyond just switching the driver.

1. First Step: Switch the Session Driver

You already mentioned updating config/session.php to set 'driver' => env('SESSION_DRIVER', 'redis') — that's a solid start. For easier environment-specific setup, though, I'd recommend just editing your root .env file and changing:

SESSION_DRIVER=file

to

SESSION_DRIVER=redis

This keeps your config files clean and lets you adjust settings per environment without code changes.

2. Critical Additional Configurations

Just flipping the driver switch isn't enough. Here's what else you need to do:

a. Install the Redis PHP Extension

Laravel needs a way to communicate with Redis. You have two options:

  • Predis: A pure-PHP Redis client that works with most PHP versions. Install it via Composer:
    composer require predis/predis
    
  • PhpRedis: The official C extension (faster, but requires system-level installation). On Ubuntu, you'd run sudo apt install php-redis; on Windows, enable the extension in your php.ini file.

b. Configure Your Redis Connection

Open config/database.php and locate the redis section. Make sure your server details match your Redis setup, and optionally create a dedicated connection for sessions to avoid mixing cache data:

'redis' => [
    'client' => env('REDIS_CLIENT', 'predis'), // Set to 'phpredis' if using that extension
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
    ],
    // Optional: Dedicated connection for sessions
    'session' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_SESSION_DB', 1), // Use database 1 for sessions
    ],
],

Then add REDIS_SESSION_DB=1 to your .env file if you're using the dedicated connection.

c. Tell Sessions to Use the Right Redis Connection

Back in config/session.php, find the 'connection' key. If you set up a dedicated session connection, update this to:

'connection' => env('SESSION_REDIS_CONNECTION', 'session'),

This ensures session data is stored separately from other Redis-backed cache, making it easier to manage and clean up.

d. Adjust Session Expiry Settings

Still in config/session.php, tweak these settings to match your app's needs:

'lifetime' => env('SESSION_LIFETIME', 120), // Session expires after 120 minutes by default
'expire_on_close' => false, // Set to true if you want sessions to end when the browser closes

3. Test It Out

After making these changes, clear your config cache to ensure Laravel picks up the new settings:

php artisan config:clear

Start your app, log in as a user, then fire up redis-cli and run KEYS laravel:session:*. If you see keys popping up, you're good to go — your sessions are now stored in Redis!

内容的提问来源于stack exchange,提问作者Spacex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:29:35