Redis.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace addons\shopro\library;
  3. use Redis as SystemRedis;
  4. class Redis
  5. {
  6. protected $handler = null;
  7. protected $options = [
  8. 'host' => '127.0.0.1',
  9. 'port' => 6379,
  10. 'password' => '',
  11. 'select' => 0,
  12. 'timeout' => 0,
  13. 'expire' => 0,
  14. 'persistent' => false,
  15. 'prefix' => '',
  16. ];
  17. /**
  18. * 构造函数
  19. * @param array $options 缓存参数
  20. * @access public
  21. */
  22. public function __construct($options = [])
  23. {
  24. if (!extension_loaded('redis')) {
  25. throw new \BadFunctionCallException('not support: redis');
  26. }
  27. // 获取 redis 配置
  28. $config = config('redis');
  29. $config = $config ?: [];
  30. $this->options = array_merge($this->options, $config);
  31. if (!empty($options)) {
  32. $this->options = array_merge($this->options, $options);
  33. }
  34. $this->handler = new SystemRedis();
  35. if ($this->options['persistent']) {
  36. $this->handler->pconnect($this->options['host'], intval($this->options['port']), $this->options['timeout'], 'persistent_id_' . $this->options['select']);
  37. } else {
  38. $this->handler->connect($this->options['host'], intval($this->options['port']), $this->options['timeout']);
  39. }
  40. if ('' != $this->options['password']) {
  41. $this->handler->auth($this->options['password']);
  42. }
  43. if (0 != $this->options['select']) {
  44. $this->handler->select(intval($this->options['select']));
  45. }
  46. }
  47. public function getRedis()
  48. {
  49. return $this->handler;
  50. }
  51. /**
  52. * 方法转发到redis
  53. *
  54. * @param string $funcname
  55. * @param array $arguments
  56. * @return void
  57. */
  58. public function __call($funcname, $arguments)
  59. {
  60. return $this->getRedis()->{$funcname}(...$arguments);
  61. }
  62. }