RedisClient.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace redis;
  3. use think\Env;
  4. /**
  5. * Class RedisClient
  6. * redis 工具类
  7. */
  8. class RedisClient
  9. {
  10. private $connect;
  11. private $cfg;
  12. /**
  13. * RedisClient constructor.
  14. * @param null $config
  15. */
  16. public function __construct($config = null)
  17. {
  18. $this->cfg = $config ? $config : [
  19. 'host' => Env::get('redis.host') ?? '127.0.0.1',
  20. 'port' => Env::get('redis.port') ?? '6379',
  21. 'password' => Env::get('redis.password') ?? null,
  22. 'select' => Env::get('redis.select') ?? 0,
  23. 'timeout' => Env::get('redis.timeout') ?? 500,
  24. ];
  25. $this->connect = new \Redis();
  26. $this->connect->connect(
  27. $this->cfg['host'],
  28. $this->cfg['port'],
  29. $this->cfg['timeout']
  30. );
  31. $this->connect->auth($this->cfg['password']);
  32. $this->connect->select($this->cfg['select']);
  33. return $this->connect;
  34. }
  35. static function of($config = null)
  36. {
  37. return (new RedisClient($config = null))->connect;
  38. }
  39. }