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. 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. print_r($options);
  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. }