TwRedis.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace tw\redis;
  3. use think\facade\Config;
  4. /**
  5. * 另一个 Redis 客户端,另一个是 Cache 使用
  6. */
  7. class TwRedis
  8. {
  9. protected static $inst;
  10. protected $handle;
  11. protected function __construct($options)
  12. {
  13. if (extension_loaded('redis')) {
  14. $this->handle = new \Redis;
  15. if ($options['persistent']) {
  16. $this->handle->pconnect($options['host'], $options['port'], $options['timeout'], 'tw_persistent_id' . $options['select']);
  17. } else {
  18. $this->handle->connect($options['host'], $options['port'], $options['timeout']);
  19. }
  20. if ('' != $options['password']) {
  21. $this->handle->auth($options['password']);
  22. }
  23. if (0 != $options['select']) {
  24. $this->handle->select($options['select']);
  25. }
  26. } else {
  27. throw new \BadFunctionCallException('not support: redis');
  28. }
  29. }
  30. public static function instance()
  31. {
  32. if (is_null(self::$inst)) {
  33. $options = Config::get('redis', []);
  34. self::$inst = new static($options);
  35. }
  36. return self::$inst;
  37. }
  38. public static function __callStatic($name, $arguments)
  39. {
  40. return self::instance()->handle->{$name}(...$arguments);
  41. }
  42. }