Base.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Redis allows implementing reliable queues, here is the spec:
  4. *
  5. * - For each queue, you have 4 different HASH:
  6. *
  7. * - One for queued items queue:NAME:queued
  8. *
  9. * - One for claimed items being processed: queue:NAME:claimed
  10. *
  11. * - One for claimed items leave time: queue:NAME:leave
  12. * Items from this one will be arbitrarily fetched at cron
  13. * time and released when leave is outdated.
  14. *
  15. * - One containing the item values and other valuable stateful
  16. * information: queue:NAME:data ;
  17. *
  18. * - For example, current job maximum identifier (auto increment
  19. * emulation) will be stored in the "sequence" HASH key
  20. *
  21. * - All other keys within the HASH will be the items themselves,
  22. * keys for those will always be numeric
  23. *
  24. * - Each time a queue will be emptied, even during a pragmatic process,
  25. * it will be automatically deleted, reseting the sequence counter to
  26. * the 0 value each time
  27. *
  28. * - Algorithm is a variation of the one described in "Reliable queue"
  29. * section of http://redis.io/commands/rpoplpush and partial port of what
  30. * you can find in the http://drupal.org/project/redis_queue module.
  31. *
  32. * You will find the driver specific implementation in the Redis_Queue_*
  33. * classes as they may differ in how the API handles transaction, pipelining
  34. * and return values.
  35. */
  36. abstract class Redis_Queue_Base extends Redis_AbstractBackend implements
  37. DrupalReliableQueueInterface
  38. {
  39. /**
  40. * Key prefix for queue data.
  41. */
  42. const QUEUE_KEY_PREFIX = 'queue';
  43. /**
  44. * Data HASH sequence key name.
  45. */
  46. const QUEUE_HKEY_SEQ = 'seq';
  47. /**
  48. * Get data HASH key
  49. *
  50. * Key will already be prefixed
  51. *
  52. * @return string
  53. */
  54. public function getKeyForData()
  55. {
  56. return $this->getKey('data');
  57. }
  58. /**
  59. * Get queued items LIST key
  60. *
  61. * Key will already be prefixed
  62. *
  63. * @return string
  64. */
  65. public function getKeyForQueue()
  66. {
  67. return $this->getKey('queued');
  68. }
  69. /**
  70. * Get claimed LIST key
  71. *
  72. * Key will already be prefixed
  73. *
  74. * @return string
  75. */
  76. public function getKeyForClaimed()
  77. {
  78. return $this->getKey('claimed');
  79. }
  80. /**
  81. * Default contructor
  82. *
  83. * Beware that DrupalQueueInterface does not defines the __construct
  84. * method in the interface yet is being used from DrupalQueue::get()
  85. *
  86. * @param mixed $client
  87. * @param string $name
  88. */
  89. public function __construct($client, $name)
  90. {
  91. parent::__construct($client, self::QUEUE_KEY_PREFIX . $name);
  92. }
  93. }