GearmanCache.php 2.8 KB

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