GearmanCache.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Mmoreramerino\GearmanBundle\Service;
  3. use Symfony\Component\DependencyInjection\ContainerAware;
  4. /**
  5. * Gearman cache class
  6. *
  7. * @author Marc Morera <marc@ulabox.com>
  8. */
  9. class GearmanCache extends ContainerAware
  10. {
  11. /**
  12. * loadCache method. While data is not setted, empty array is loaded.
  13. * Sets dir and checks it
  14. */
  15. public function loadCache()
  16. {
  17. $this->getPath();
  18. $this->checkDir();
  19. $this->data = $this->__toCache(array());
  20. }
  21. /**
  22. * Cache dir to save all Gearman cached files.
  23. *
  24. * @var string
  25. */
  26. private $cachedir;
  27. /**
  28. * Cache filename
  29. *
  30. * @var string
  31. */
  32. private $cachefile = 'gearman.cache.php';
  33. /**
  34. * Data loaded and serialized ready for save into cache
  35. *
  36. * @var string
  37. */
  38. private $data = '';
  39. /**
  40. * Checks is cache dir is created.
  41. * Otherwise, it creates it
  42. * Returns self object
  43. *
  44. * @return GearmanCache
  45. */
  46. public function checkDir()
  47. {
  48. if (!is_dir($this->cachedir)) {
  49. mkdir($this->cachedir, 0777, true);
  50. }
  51. return $this;
  52. }
  53. /**
  54. * Remove gearman cache file
  55. * Returns self object
  56. *
  57. * @return GearmanCache
  58. */
  59. public function emptyCache()
  60. {
  61. if (is_dir($this->cachedir . $this->cachefile)) {
  62. unlink($this->cachedir . $this->cachefile);
  63. }
  64. return $this;
  65. }
  66. /**
  67. * Returns if cache file exists
  68. *
  69. * @return boolean
  70. */
  71. public function existsCacheFile()
  72. {
  73. return is_file($this->cachedir . $this->cachefile);
  74. }
  75. /**
  76. * Save to cache all gearman cacheable data
  77. * Returns self object
  78. *
  79. * @param array $array Data to set into cache
  80. *
  81. * @return GearmanCache
  82. */
  83. public function set(Array $array)
  84. {
  85. $this->data = "<?php return unserialize('".serialize($array)."');";
  86. file_put_contents($this->cachedir . $this->cachefile, $this->data);
  87. return $this;
  88. }
  89. /**
  90. * Save data into cache
  91. * Returns self object
  92. *
  93. * @return boolean Return if saved
  94. */
  95. public function save()
  96. {
  97. return file_put_contents($this->cachedir . $this->cachefile, $this->data);
  98. }
  99. /**
  100. * Retrieve cache data if is loaded
  101. * if cache does not exist, return false
  102. *
  103. * @return Array
  104. */
  105. public function get()
  106. {
  107. if (is_file($this->cachedir . $this->cachefile)) {
  108. return (array) (require($this->cachedir . $this->cachefile));
  109. }
  110. return false;
  111. }
  112. /**
  113. * Transform Array object into cache saveable string
  114. *
  115. * @param Array $data Data to set into cache
  116. *
  117. * @return string
  118. */
  119. public function __toCache(Array $data)
  120. {
  121. return "<?php return unserialize('".serialize($data)."');";
  122. }
  123. /**
  124. * Return cache dir
  125. *
  126. * @return string
  127. */
  128. public function getPath()
  129. {
  130. if (null === $this->cachedir) {
  131. $rootDir = $this->container->get('kernel')->getRootDir();
  132. $this->cachedir = $rootDir . '/cache/'.$this->container->get('kernel')->getEnvironment().'/gearman/';
  133. }
  134. return $this->cachedir;
  135. }
  136. }