GearmanCacheWrapper.php 4.4 KB

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