在 Laravel 7 中配置 PhpRedis

Configuring PhpRedis in Laravel 7

我已经在 Homestead 中设置了 Laravel 的全新安装,并且我已经按照 Laravel 文档 https://laravel.com/docs/7.x/redis#phpredis.

中的建议安装了 PhpRedis

我按照本指南安装 PhpRedis https://webstoked.com/install-phpredis-laravel-ubuntu/

在 Laravel 文档和我链接的安装 PhpRedis 的指南中,我被指示重命名 config/app.php.[= 中的 Redis 别名15=]

If you plan to use PhpRedis extension along with the Redis Facade alias, you should rename it to something else, like RedisManager, to avoid a collision with the Redis class. You can do that in the aliases section of your app.php config file.
- Laravel Docs

为了进一步增加我的困惑,Laravel 文档继续说您应该完全删除别名。

To avoid class naming collisions with the Redis PHP extension itself, you will need to delete or rename the Illuminate\Support\Facades\Redis facade alias from your app configuration file's aliases array. Generally, you should remove this alias entirely and only reference the facade by its fully qualified class name while using the Redis PHP extension.
- Laravel Docs

我的主要问题是:

  1. "If you plan to use PhpRedis extension along with the Redis Facade alias" 是什么意思?
  2. 我什么时候应该重命名别名、删除它或保持原样?
  3. 根据我是重命名还是删除别名,这对使用 Redis 有何影响?

在 laravel 项目中有两个不同的 configurations/ways 可以使用 redis

  1. 第一个是使用 predis,它在您的 vendor 文件夹中。这个 "Flexible and feature-complete Redis client for PHP and HHVM" 位于 here。是package/library写在php.
  2. 另一种方法是使用 PhpRedis,它是用 C 编写的扩展,位于 here.
protected function connector()
{
    switch ($this->driver) {
        case 'predis':
            return new Connectors\PredisConnector;
        case 'phpredis':
            return new Connectors\PhpRedisConnector;
    }
}

  1. "If you plan to use PhpRedis extension along with the Redis Facade alias" 是什么意思?

    In the framework there is a check. While creating the PhpRedis client of Redis, it is checking whether the new Redis instance is Facade because PhpRedis is also using Redis name is you can see from here. So if you want to use PhpRedis in your laravel framework you better rename your facade because it will cause collision.

  2. 我什么时候应该重命名别名、删除它或保持原样?

    If you are going to use predis as client, then you can leave it as-is. If you are going to use PhpRedis as client, then you need to rename alias.

  3. 根据我是否重命名或删除别名,这对使用 Redis 有何影响?

    You will use RedisManager::someMethod() if you choose PhpRedis. You will use Redis::someMethod() if you use predis.