Home > PHP Framework > ThinkPHP > body text

About the implementation of multiple Redis classes in ThinkPHP6

藏色散人
Release: 2020-09-08 09:51:41
forward
5492 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

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('user:1', 'userName', 'admin');

Redis::instance(1)->hSet('user', 'name', 'admin1');
Redis::instance(2)->hSet('user', 'name', 'admin2');
Redis::instance(3)->hSet('user', 'name', 'admin3');
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!