5 * Definition of Drush\Cache\FileCache.
11 use Symfony\Component\Filesystem\Filesystem;
12 use Symfony\Component\Finder\Finder;
13 use Webmozart\PathUtil\Path;
16 * Default cache implementation.
18 * This cache implementation uses plain text files
19 * containing serialized php to store cached data. Each cache bin corresponds
20 * to a directory by the same name.
24 class FileCache implements CacheInterface
26 const EXTENSION = '.cache';
29 public function __construct($bin)
32 $this->directory = $this->cacheDirectory();
36 * Returns the cache directory for the given bin.
40 public function cacheDirectory($bin = null)
42 $bin = $bin ? $bin : $this->bin;
43 return Path::join(Drush::config()->cache(), $bin);
46 public function get($cid)
49 $cache = $this->getMultiple($cids);
53 public function getMultiple(&$cids)
57 foreach ($cids as $cid) {
58 $filename = $this->getFilePath($cid);
59 if (!file_exists($filename)) {
63 $item = $this->readFile($filename);
68 $cids = array_diff($cids, array_keys($cache));
70 } catch (\Exception $e) {
76 * Returns the contents of the given filename unserialized.
78 * @param string $filename
79 * Absolute path to filename to read contents from.
81 public function readFile($filename)
83 $item = file_get_contents($filename);
84 return $item ? unserialize($item) : false;
87 public function set($cid, $data, $expire = DRUSH_CACHE_PERMANENT)
91 $cache = new \stdClass;
93 $cache->data = is_object($data) ? clone $data : $data;
94 $cache->created = $created;
95 if ($expire == DRUSH_CACHE_TEMPORARY) {
96 $cache->expire = $created + 2591999;
97 } // Expire time is in seconds if less than 30 days, otherwise is a timestamp.
98 elseif ($expire != DRUSH_CACHE_PERMANENT && $expire < 2592000) {
99 $cache->expire = $created + $expire;
101 $cache->expire = $expire;
104 // Ensure the cache directory still exists, in case a backend process
105 // cleared the cache after the cache was initialized.
106 $fs = new Filesystem();
107 $fs->mkdir($this->directory);
109 $filename = $this->getFilePath($cid);
110 return $this->writeFile($filename, $cache);
114 * Serializes data and write it to the given filename.
116 * @param string $filename
117 * Absolute path to filename to write cache data.
119 * Cache data to serialize and write to $filename.
121 public function writeFile($filename, $cache)
123 return file_put_contents($filename, serialize($cache));
126 public function clear($cid = null, $wildcard = false)
128 $fs = new Filesystem();
129 $bin_dir = $this->cacheDirectory();
132 $fs->remove($bin_dir);
136 $fs->remove($bin_dir);
138 $files = Finder::create()
144 $files[] = $this->getFilePath($cid);
151 public function isEmpty()
153 $files = Finder::create()
158 ->in($this->directory);
159 return empty($files);
163 * Converts a cache id to a full path.
166 * The cache ID of the data to retrieve.
169 * The full path to the cache file.
171 protected function getFilePath($cid)
173 return $this->directory . '/' . str_replace([':', '\\', '/'], '.', $cid) . self::EXTENSION;