Queue.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Heanup\Frame\Package;
  3. /**
  4. * 队列
  5. * 使用redis实现
  6. */
  7. class Queue extends \Heanup\Frame\Package
  8. {
  9. const CONFIG_CLASS = 'Redis\\Queue';
  10. const PREFIX = '';
  11. /**
  12. * 获取队列长度
  13. * @return int
  14. */
  15. public static function length()
  16. {
  17. $key = self::getKey();
  18. $result = self::getConnection(true)->lLen($key);
  19. return $result;
  20. }
  21. /**
  22. * 插入数据
  23. * @param $data
  24. * @return int
  25. */
  26. public static function push($data)
  27. {
  28. $key = self::getKey();
  29. $result = self::getConnection(true)->rPush($key, $data);
  30. return $result;
  31. }
  32. /**
  33. * 取出数据
  34. * @return string
  35. */
  36. public static function pop()
  37. {
  38. $key = self::getKey();
  39. $result = self::getConnection(true)->lPop($key);
  40. return $result;
  41. }
  42. /**
  43. * 获取连接池
  44. * @param bool $isMaster
  45. * @return \Redis
  46. */
  47. protected static function getConnection($isMaster = false)
  48. {
  49. $class = get_called_class();
  50. $configClass = $class::CONFIG_CLASS;
  51. $redis = new \Heanup\Frame\Library\Redis($configClass);
  52. $connection = $redis->getConnection($isMaster);
  53. return $connection;
  54. }
  55. }