GearmanCacheWrapper.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * @author Marc Morera <yuhu@mmoreram.com>
  6. * @since 2013
  7. */
  8. namespace Mmoreram\GearmanBundle\Service;
  9. use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
  10. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  11. use Doctrine\Common\Cache\Cache;
  12. /**
  13. * Gearman cache loader class
  14. *
  15. * This class has responsability of loading all gearman data structure
  16. * and cache it if needed.
  17. *
  18. * Also provides this data to external services
  19. *
  20. * @author Marc Morera <yuhu@mmoreram.com>
  21. */
  22. class GearmanCacheWrapper implements CacheClearerInterface, CacheWarmerInterface
  23. {
  24. /**
  25. * @var GearmanParser
  26. *
  27. * Gearman file parser
  28. */
  29. private $gearmanParser;
  30. /**
  31. * @var Cache
  32. *
  33. * Cache instance
  34. */
  35. private $cache;
  36. /**
  37. * @var string
  38. *
  39. * Cache id
  40. */
  41. private $cacheId;
  42. /**
  43. * @var array
  44. *
  45. * WorkerCollection with all workers and jobs available
  46. */
  47. private $workerCollection;
  48. /**
  49. * Construct method
  50. *
  51. * @param GearmanParser $gearmanParser Gearman Parser
  52. * @param Cache $cache Cache instance
  53. * @param string $cacheId Cache id
  54. */
  55. public function __construct(GearmanParser $gearmanParser, Cache $cache, $cacheId)
  56. {
  57. $this->gearmanParser = $gearmanParser;
  58. $this->cache = $cache;
  59. $this->cacheId = $cacheId;
  60. }
  61. /**
  62. * Return gearman file parser
  63. *
  64. * @return GearmanParser
  65. */
  66. public function getGearmanParser()
  67. {
  68. return $this->gearmanParser;
  69. }
  70. /**
  71. * Return cache
  72. *
  73. * @return Cache Cache
  74. */
  75. public function getCache()
  76. {
  77. return $this->cache;
  78. }
  79. /**
  80. * Return cache id
  81. *
  82. * @return string Cache id
  83. */
  84. public function getCacheId()
  85. {
  86. return $this->cacheId;
  87. }
  88. /**
  89. * Return workerCollection
  90. *
  91. * @return array all available workers
  92. */
  93. public function getWorkers()
  94. {
  95. return $this->workerCollection;
  96. }
  97. /**
  98. * loads Gearman cache, only if is not loaded yet
  99. *
  100. * @param Cache $cache Cache instance
  101. * @param string $cacheId Cache id
  102. *
  103. * @return GearmanCacheWrapper self Object
  104. */
  105. public function load(Cache $cache, $cacheId)
  106. {
  107. if ($cache->contains($cacheId)) {
  108. /**
  109. * Cache contains gearman structure
  110. */
  111. $this->workerCollection = $cache->fetch($cacheId);
  112. } else {
  113. /**
  114. * Cache is empty.
  115. *
  116. * Full structure must be generated and cached
  117. */
  118. $this->workerCollection = $this
  119. ->getGearmanParser()
  120. ->load()
  121. ->toArray();
  122. $cache->save($cacheId, $this->workerCollection);
  123. }
  124. return $this;
  125. }
  126. /**
  127. * flush all cache
  128. *
  129. * @param Cache $cache Cache instance
  130. * @param string $cacheId Cache id
  131. *
  132. * @return GearmanCacheWrapper self Object
  133. */
  134. public function flush(Cache $cache, $cacheId)
  135. {
  136. $cache->delete($cacheId);
  137. return $this;
  138. }
  139. /**
  140. * Cache clear implementation
  141. *
  142. * @param string $cacheDir The cache directory
  143. *
  144. * @return GearmanCacheWrapper self Object
  145. */
  146. public function clear($cacheDir)
  147. {
  148. $this->flush($this->getCache(), $this->getCacheId());
  149. return $this;
  150. }
  151. /**
  152. * Warms up the cache.
  153. *
  154. * @param string $cacheDir The cache directory
  155. *
  156. * @return GearmanCacheWrapper self Object
  157. */
  158. public function warmUp($cacheDir)
  159. {
  160. $this->load($this->getCache(), $this->getCacheId());
  161. return $this;
  162. }
  163. /**
  164. * Checks whether this warmer is optional or not.
  165. *
  166. * Optional warmers can be ignored on certain conditions.
  167. *
  168. * A warmer should return true if the cache can be
  169. * generated incrementally and on-demand.
  170. *
  171. * As GearmanBundle loads cache incrementaly so is optional
  172. *
  173. * @return Boolean true if the warmer is optional, false otherwise
  174. */
  175. public function isOptional()
  176. {
  177. return true;
  178. }
  179. }