FileCache.php

  1. 8.0.x lib/Drush/Cache/FileCache.php
  2. 7.x lib/Drush/Cache/FileCache.php
  3. master lib/Drush/Cache/FileCache.php

Definition of Drush\Cache\FileCache.

Namespace

Drush\Cache

Classes

Namesort descending Description
FileCache Default cache implementation.

File

lib/Drush/Cache/FileCache.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drush\Cache\FileCache.
  5. */
  6. namespace Drush\Cache;
  7. /**
  8. * Default cache implementation.
  9. *
  10. * This cache implementation uses plain text files
  11. * containing serialized php to store cached data. Each cache bin corresponds
  12. * to a directory by the same name.
  13. */
  14. class FileCache implements CacheInterface {
  15. const EXTENSION = '.cache';
  16. protected $bin;
  17. function __construct($bin) {
  18. $this->bin = $bin;
  19. $this->directory = $this->cacheDirectory();
  20. }
  21. /**
  22. * Returns the cache directory for the given bin.
  23. *
  24. * @param string $bin
  25. */
  26. function cacheDirectory($bin = NULL) {
  27. $bin = $bin ? $bin : $this->bin;
  28. return drush_directory_cache($bin);
  29. }
  30. function get($cid) {
  31. $cids = array($cid);
  32. $cache = $this->getMultiple($cids);
  33. return reset($cache);
  34. }
  35. function getMultiple(&$cids) {
  36. try {
  37. $cache = array();
  38. foreach ($cids as $cid) {
  39. $filename = $this->getFilePath($cid);
  40. if (!file_exists($filename)) throw new \Exception;
  41. $item = $this->readFile($filename);
  42. if ($item) {
  43. $cache[$cid] = $item;
  44. }
  45. }
  46. $cids = array_diff($cids, array_keys($cache));
  47. return $cache;
  48. }
  49. catch (\Exception $e) {
  50. return array();
  51. }
  52. }
  53. /**
  54. * Returns the contents of the given filename unserialized.
  55. *
  56. * @param string $filename
  57. * Absolute path to filename to read contents from.
  58. */
  59. function readFile($filename) {
  60. $item = file_get_contents($filename);
  61. return $item ? unserialize($item) : FALSE;
  62. }
  63. function set($cid, $data, $expire = DRUSH_CACHE_PERMANENT) {
  64. $created = time();
  65. $cache = new \stdClass;
  66. $cache->cid = $cid;
  67. $cache->data = is_object($data) ? clone $data : $data;
  68. $cache->created = $created;
  69. if ($expire == DRUSH_CACHE_TEMPORARY) {
  70. $cache->expire = $created + 2591999;
  71. }
  72. // Expire time is in seconds if less than 30 days, otherwise is a timestamp.
  73. elseif ($expire != DRUSH_CACHE_PERMANENT && $expire < 2592000) {
  74. $cache->expire = $created + $expire;
  75. }
  76. else {
  77. $cache->expire = $expire;
  78. }
  79. // Ensure the cache directory still exists, in case a backend process
  80. // cleared the cache after the cache was initialized.
  81. drush_mkdir($this->directory);
  82. $filename = $this->getFilePath($cid);
  83. return $this->writeFile($filename, $cache);
  84. }
  85. /**
  86. * Serializes data and write it to the given filename.
  87. *
  88. * @param string $filename
  89. * Absolute path to filename to write cache data.
  90. * @param $cache
  91. * Cache data to serialize and write to $filename.
  92. */
  93. function writeFile($filename, $cache) {
  94. return file_put_contents($filename, serialize($cache));
  95. }
  96. function clear($cid = NULL, $wildcard = FALSE) {
  97. $bin_dir = $this->cacheDirectory();
  98. $files = array();
  99. if (empty($cid)) {
  100. drush_delete_dir($bin_dir, TRUE);
  101. }
  102. else {
  103. if ($wildcard) {
  104. if ($cid == '*') {
  105. drush_delete_dir($bin_dir, TRUE);
  106. }
  107. else {
  108. $matches = drush_scan_directory($bin_dir, "/^$cid/", array('.', '..'));
  109. $files = $files + array_keys($matches);
  110. }
  111. }
  112. else {
  113. $files[] = $this->getFilePath($cid);
  114. }
  115. foreach ($files as $f) {
  116. if (file_exists($f)) {
  117. unlink($f);
  118. }
  119. }
  120. }
  121. }
  122. function isEmpty() {
  123. $files = drush_scan_directory($this->directory, "//", array('.', '..'));
  124. return empty($files);
  125. }
  126. /**
  127. * Converts a cache id to a full path.
  128. *
  129. * @param $cid
  130. * The cache ID of the data to retrieve.
  131. *
  132. * @return
  133. * The full path to the cache file.
  134. */
  135. protected function getFilePath($cid) {
  136. return $this->directory . '/' . str_replace(array(':'), '.', $cid) . self::EXTENSION;
  137. }
  138. }