Home > PHP Framework > ThinkPHP > About the implementation of multiple Redis classes in ThinkPHP6

About the implementation of multiple Redis classes in ThinkPHP6

藏色散人
Release: 2020-09-08 09:51:41
forward
5695 people have browsed it

The following tutorial column will introduce you to multiple Redis class implementations in ThinkPHP6. I hope it will be helpful to friends in need!

About the implementation of multiple Redis classes in ThinkPHP6

#Encapsulate a Redis multi-project in the Thinkphp project Library singleton operation class

1. Preparation before operation

If the phpredis module is not installed, execute it first

composer require predis/predis
Copy after login
2. Configure Redis connection information

Configure in app\config\cache.php

'redis' => [
   // 驱动方式
    'type'       => 'redis',
    // 连接地址
    'host'       => Env::get('redis.host'),
    // 端口
    'port'       => Env::get('redis.port'),],
Copy after login
More configuration reference
/**
 * 配置参数
 * @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'  => [],];
Copy after login

Configure connection information in .env

[REDIS]host = 127.0.0.1
port = 6379
Copy after login

3. Write code

Create the file Redis.php under app\common

<?phpnamespace app\common;use think\facade\Config;use think\cache\driver\redis as ThinkRedis;class Redis extends ThinkRedis{
    /**
     * @var int
     */
    protected $hash;

    /**
     * @var array
     */
    protected static $instance = [];

    /**
     * Redis constructor.
     * @param $db
     */
    private function __construct($db)
    {
        $options = Config::get(&#39;cache.stores.redis&#39;);
        $options[&#39;select&#39;] = $db;
        $this->hash = $db;
        $this->options = array_merge($this->options, $options);
        parent::__construct();
    }

    private function __clone()
    {
    }

    /**
     * @param int $db
     * @return \Predis\Client|\Redis
     */
    public static function instance($db = 0)
    {
        if (! isset(self::$instance[$db])) {
            self::$instance[$db] = new self($db);
        }

        return self::$instance[$db];
    }

    public function __destruct()
    {
        self::$instance[$this->hash]->close();
        unset(self::$instance[$this->hash]);
    }}
Copy after login
4. How to use

use app\common\Redis;

$redis = Redis::instance(4);
$redis->hSet(&#39;user:1&#39;, &#39;userName&#39;, &#39;admin&#39;);

Redis::instance(1)->hSet(&#39;user&#39;, &#39;name&#39;, &#39;admin1&#39;);
Redis::instance(2)->hSet(&#39;user&#39;, &#39;name&#39;, &#39;admin2&#39;);
Redis::instance(3)->hSet(&#39;user&#39;, &#39;name&#39;, &#39;admin3&#39;);
Copy after login

For more usage methods, please refer to

redis command manual

The above is the detailed content of About the implementation of multiple Redis classes in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template