Lumen 5.5 中使用 Redis Cache

PHP 应已安装 PECL 的 PhpRedis 扩展

  1. 安装 predis/predis 扩展和 illuminate/redis 扩展

    composer require predis/predis
    composer require illuminate/redis 5.5.*
  2. bootstrap/app.php 中开启 Facade

     $app->withFacades();
  3. bootstrap/app.php 中加载配置文件

    $app->configure('database');
    $app->configure('cache');
  4. bootstrap/app.php 中注册 RedisServiceProvider

    $app->register(Illuminate\Redis\RedisServiceProvider::class);
  5. config/cache.php 中添加配置

    'default' => env('CACHE_DRIVER', 'redis'),
    'stores' => [
            'redis' => [
                'driver' => 'redis',
                'connection' => env('CACHE_REDIS_CONNECTION', 'cache'),
            ]
        ]
  6. config/database.php 中添加配置

    'redis' => [
        'client' => 'predis',
        'cache' => [
            'host'     => env('REDIS_HOST', '127.0.0.1'),
            'port'     => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DATABASE', 0),
            'password' => env('REDIS_PASSWORD', null),
        ]
    ]

参考 Laravel 5.5 文档 CacheRedis 以及 Lumen 5.5 文档 Cache