参考文献

ThinkPHP6多例Redis类实现
Redis 官方命令文档
php使用redis的有序集合zset实现延迟队列
基于 Redis 实现延迟队列

环境说明

  • ThinkPHP 6.0
  • Redis
  • PHP 7.4.11
  • Homestead

步骤说明

安装 predis/predis 依赖

composer require predis/predis
注:安装该扩展目的详看:\think\cache\driver\Redis

if (extension_loaded('redis')) {
    $this->handler = new \Redis;

    if ($this->options['persistent']) {
        $this->handler->pconnect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout'], 'persistent_id_' . $this->options['select']);
    } else {
        $this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $this->options['timeout']);
    }

    if ('' != $this->options['password']) {
        $this->handler->auth($this->options['password']);
    }
} elseif (class_exists('\Predis\Client')) {
    $params = [];
    foreach ($this->options as $key => $val) {
        if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) {
            $params[$key] = $val;
            unset($this->options[$key]);
        }
    }

    if ('' == $this->options['password']) {
        unset($this->options['password']);
    }

    $this->handler = new \Predis\Client($this->options, $params);

    $this->options['prefix'] = '';
} else {
    throw new \BadFunctionCallException('not support: redis');
}

配置 Redis

配置app\config\cache.php如下图

<?php

use think\facade\Env;

// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------

return [
    // 默认缓存驱动
    'default' => Env::get('cache.driver', 'file'),

    // 缓存连接方式配置
    'stores'  => [
        'file'  => [
            // 驱动方式
            'type'       => 'File',
            // 缓存保存目录
            'path'       => '',
            // 缓存前缀
            'prefix'     => '',
            // 缓存有效期 0表示永久缓存
            'expire'     => 0,
            // 缓存标签前缀
            'tag_prefix' => 'tag:',
            // 序列化机制 例如 ['serialize', 'unserialize']
            'serialize'  => [],
        ],
        // 更多的缓存连接
        'redis' => [
            'type'       => 'redis',
            'host'       => Env::get('redis.host', '127.0.0.1'),                                  // redis 主机ip
            'port'       => Env::get('redis.port', 6379),                                         // redis 端口
            'password'   => Env::get('redis.password', ''),                                       // redis 密码
            'select'     => Env::get('redis.select', 0),                                          // 使用db,默认为 db0
            'timeout'    => Env::get('redis.timeout', 0),                                         // redis连接的超时时间
            'expire'     => Env::get('redis.expire', 0),                                          // 缓存有效期 0表示永久缓存
            'persistent' => Env::get('redis.persistent', false),                                  // 是否持久链接
            'prefix'     => Env::get('redis.prefix', 'prefix'),                                   // 前缀
            'tag_prefix' => Env::get('redis.tag_prefix', 'tag:'),                                 // 缓存标签前缀
            'serialize'  => explode(',', Env::get('redis.serialize', [])),                        // 序列化机制 例如 ['serialize', 'unserialize']
        ],
    ],
];

注:详细配置参数
参考think\cache\driver\Redis中如下配置

/**
* 配置参数
* @var array
*/
protected $options = [
    'host'       => '127.0.0.1',
    'port'       => 6379,
    'password'   => '',
    'select'     => 0,
    'timeout'    => 0,
    'expire'     => 0,
    'persistent' => false,
    'prefix'     => '',
    'tag_prefix' => 'tag:',
    'serialize'  => [],
];