| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace redis;
- use think\Env;
- /**
- * Class RedisClient
- * redis 工具类
- */
- class RedisClient
- {
- private $connect;
- private $cfg;
- /**
- * RedisClient constructor.
- * @param null $config
- */
- public function __construct($config = null)
- {
- $this->cfg = $config ? $config : [
- 'host' => Env::get('redis.host') ?? '127.0.0.1',
- 'port' => Env::get('redis.port') ?? '6379',
- 'password' => Env::get('redis.password') ?? null,
- 'select' => Env::get('redis.select') ?? 0,
- 'timeout' => Env::get('redis.timeout') ?? 500,
- ];
- $this->connect = new \Redis();
- $this->connect->connect(
- $this->cfg['host'],
- $this->cfg['port'],
- $this->cfg['timeout']
- );
- $this->connect->auth($this->cfg['password']);
- $this->connect->select($this->cfg['select']);
- return $this->connect;
- }
- static function of($config = null)
- {
- return (new RedisClient($config = null))->connect;
- }
- }
|