RedisQueue.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Heanup\Frame\Package;
  3. /**
  4. * @author cxm@meitu.com
  5. * 队列
  6. */
  7. class RedisQueue
  8. {
  9. protected static $_instance = NULL;
  10. protected $redisObj = NULL;
  11. protected $key = NULL;
  12. /**
  13. *
  14. * @param unknown $key
  15. * @param string $config
  16. * @return self
  17. */
  18. public static function getInstance($key, $config = "Redis\\Queue")
  19. {
  20. if (empty($key)) {
  21. return false;
  22. }
  23. if (!isset(self::$_instance[$key][$config]) || !(self::$_instance[$key][$config] instanceof self)) {
  24. self::$_instance[$key][$config] = new self($key, $config);
  25. }
  26. return self::$_instance[$key][$config];
  27. }
  28. /**
  29. * 获取队列长度
  30. * @return int
  31. */
  32. public function length()
  33. {
  34. $result = $this->getConnection(true)->lLen($this->key);
  35. return $result;
  36. }
  37. /**
  38. * 插入数据
  39. * @param $data
  40. * @return int
  41. */
  42. public function push($data)
  43. {
  44. $result = $this->getConnection(true)->lPush($this->key, $data);
  45. return $result;
  46. }
  47. /**
  48. * 取出数据
  49. * @return string
  50. */
  51. public function pop()
  52. {
  53. $result = $this->getConnection(true)->rPop($this->key);
  54. return $result;
  55. }
  56. public function delete()
  57. {
  58. $result = $this->getConnection(true)->delete($this->key);
  59. return $result;
  60. }
  61. protected function __construct($key, $config)
  62. {
  63. $this->key = $key;
  64. $this->redisObj = new \Heanup\Frame\Library\Redis($config);
  65. }
  66. protected function getConnection($isMaster)
  67. {
  68. $connection = $this->redisObj->getConnection($isMaster);
  69. return $connection;
  70. }
  71. }