-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FileCache.php
175 lines (156 loc) · 4.46 KB
/
FileCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* @file
* Definition of Drush\Cache\FileCache.
*/
namespace Drush\Cache;
use Drush\Drush;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Webmozart\PathUtil\Path;
/**
* Default cache implementation.
*
* This cache implementation uses plain text files
* containing serialized php to store cached data. Each cache bin corresponds
* to a directory by the same name.
*
* @deprecated
*/
class FileCache implements CacheInterface
{
const EXTENSION = '.cache';
protected $bin;
public function __construct($bin)
{
$this->bin = $bin;
$this->directory = $this->cacheDirectory();
}
/**
* Returns the cache directory for the given bin.
*
* @param string $bin
*/
public function cacheDirectory($bin = null)
{
$bin = $bin ? $bin : $this->bin;
return Path::join(Drush::config()->cache(), $bin);
}
public function get($cid)
{
$cids = [$cid];
$cache = $this->getMultiple($cids);
return reset($cache);
}
public function getMultiple(&$cids)
{
try {
$cache = [];
foreach ($cids as $cid) {
$filename = $this->getFilePath($cid);
if (!file_exists($filename)) {
return [];
}
$item = $this->readFile($filename);
if ($item) {
$cache[$cid] = $item;
}
}
$cids = array_diff($cids, array_keys($cache));
return $cache;
} catch (\Exception $e) {
return [];
}
}
/**
* Returns the contents of the given filename unserialized.
*
* @param string $filename
* Absolute path to filename to read contents from.
*/
public function readFile($filename)
{
$item = file_get_contents($filename);
return $item ? unserialize($item) : false;
}
public function set($cid, $data, $expire = DRUSH_CACHE_PERMANENT)
{
$created = time();
$cache = new \stdClass;
$cache->cid = $cid;
$cache->data = is_object($data) ? clone $data : $data;
$cache->created = $created;
if ($expire == DRUSH_CACHE_TEMPORARY) {
$cache->expire = $created + 2591999;
} elseif ($expire != DRUSH_CACHE_PERMANENT && $expire < 2592000) {
// Expire time is in seconds if less than 30 days, otherwise is a timestamp.
$cache->expire = $created + $expire;
} else {
$cache->expire = $expire;
}
// Ensure the cache directory still exists, in case a backend process
// cleared the cache after the cache was initialized.
$fs = new Filesystem();
$fs->mkdir($this->directory);
$filename = $this->getFilePath($cid);
return $this->writeFile($filename, $cache);
}
/**
* Serializes data and write it to the given filename.
*
* @param string $filename
* Absolute path to filename to write cache data.
* @param $cache
* Cache data to serialize and write to $filename.
*/
public function writeFile($filename, $cache)
{
return file_put_contents($filename, serialize($cache));
}
public function clear($cid = null, $wildcard = false)
{
$fs = new Filesystem();
$bin_dir = $this->cacheDirectory();
$files = [];
if (empty($cid)) {
$fs->remove($bin_dir);
} else {
if ($wildcard) {
if ($cid == '*') {
$fs->remove($bin_dir);
} else {
$files = Finder::create()
->files()
->name($cid)
->in($bin_dir);
}
} else {
$files[] = $this->getFilePath($cid);
}
$fs->remove($files);
}
}
public function isEmpty()
{
$files = Finder::create()
->files()
->name()
->exclude()
->depth(0)
->in($this->directory);
return empty($files);
}
/**
* Converts a cache id to a full path.
*
* @param $cid
* The cache ID of the data to retrieve.
*
* @return
* The full path to the cache file.
*/
protected function getFilePath($cid)
{
return $this->directory . '/' . str_replace([':', '\\', '/'], '.', $cid) . self::EXTENSION;
}
}