TwRedis.php 1.3 KB

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