1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NilPortugues\Cache\Adapter; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use NilPortugues\Cache\CacheAdapter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class FileSystemAdapter |
11
|
|
|
* @package NilPortugues\Cache\Adapter |
12
|
|
|
*/ |
13
|
|
|
class FileSystemAdapter extends Adapter implements CacheAdapter |
14
|
|
|
{ |
15
|
|
|
const CACHE_FILE_SUFFIX = '.php.cache'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $cacheDir; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $cacheDir |
24
|
|
|
* @param CacheAdapter $next |
25
|
|
|
* |
26
|
|
|
* @throws \InvalidArgumentException |
27
|
|
|
*/ |
28
|
|
|
public function __construct($cacheDir, CacheAdapter $next = null) |
29
|
|
|
{ |
30
|
|
|
$this->nextAdapter = (InMemoryAdapter::getInstance() === $next) ? null : $next; |
31
|
|
|
|
32
|
|
|
$cacheDir = \realpath($cacheDir); |
33
|
|
|
|
34
|
|
|
if (false === \is_dir($cacheDir)) { |
35
|
|
|
throw new InvalidArgumentException( |
36
|
|
|
\sprintf('The provided path %s is not a valid directory', $cacheDir) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (false === \is_writable($cacheDir)) { |
41
|
|
|
throw new InvalidArgumentException( |
42
|
|
|
\sprintf('The provided directory %s is not writable', $cacheDir) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->cacheDir = $cacheDir; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get a value identified by $key. |
51
|
|
|
* |
52
|
|
|
* @param string $key |
53
|
|
|
* |
54
|
|
|
* @return bool|mixed |
55
|
|
|
*/ |
56
|
|
|
public function get($key) |
57
|
|
|
{ |
58
|
|
|
$key = (string)$key; |
59
|
|
|
$this->hit = false; |
60
|
|
|
|
61
|
|
|
$inMemoryValue = InMemoryAdapter::getInstance()->get($key); |
62
|
|
|
if (InMemoryAdapter::getInstance()->isHit()) { |
63
|
|
|
$this->hit = true; |
64
|
|
|
return $inMemoryValue; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$fileKey = $this->getFilenameFromCacheKey($key); |
68
|
|
|
|
69
|
|
|
if (true === \file_exists($fileKey)) { |
70
|
|
|
$value = $this->restoreDataStructure(\file_get_contents($fileKey)); |
71
|
|
|
if ($value['expires'] >= (new DateTime())) { |
72
|
|
|
$this->hit = true; |
73
|
|
|
InMemoryAdapter::getInstance()->set($key, $value['value'], 0); |
74
|
|
|
return $value['value']; |
75
|
|
|
} |
76
|
|
|
$this->removeCacheFile($fileKey); |
77
|
|
|
$this->deleteChain($key); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return (null !== $this->nextAdapter) ? $this->nextAdapter->get($key) : null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $key |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
private function getFilenameFromCacheKey($key) |
89
|
|
|
{ |
90
|
|
|
return $this->cacheDir |
91
|
|
|
. DIRECTORY_SEPARATOR |
92
|
|
|
. $this->getDirectoryHash($key) |
93
|
|
|
. DIRECTORY_SEPARATOR |
94
|
|
|
. $key |
95
|
|
|
. self::CACHE_FILE_SUFFIX; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $key |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
private function getDirectoryHash($key) |
104
|
|
|
{ |
105
|
|
|
$key = \md5($key); |
106
|
|
|
|
107
|
|
|
$level1 = \substr($key, 0, 1); |
108
|
|
|
$level2 = \substr($key, 1, 1); |
109
|
|
|
$level3 = \substr($key, 2, 1); |
110
|
|
|
|
111
|
|
|
$directoryHash = $level1 . DIRECTORY_SEPARATOR . $level2 . DIRECTORY_SEPARATOR . $level3; |
112
|
|
|
$this->createCacheHashDirectory($directoryHash); |
113
|
|
|
|
114
|
|
|
return $directoryHash; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $directoryHash |
119
|
|
|
*/ |
120
|
|
|
private function createCacheHashDirectory($directoryHash) |
121
|
|
|
{ |
122
|
|
|
$cacheDir = $this->cacheDir . DIRECTORY_SEPARATOR . $directoryHash; |
123
|
|
|
if (false === \file_exists($cacheDir)) { |
124
|
|
|
\mkdir($cacheDir, 0755, true); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $fileKey |
130
|
|
|
* |
131
|
|
|
* @throws \RuntimeException |
132
|
|
|
*/ |
133
|
|
|
private function removeCacheFile($fileKey) |
134
|
|
|
{ |
135
|
|
|
\unlink($fileKey); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set a value identified by $key and with an optional $ttl. |
140
|
|
|
* |
141
|
|
|
* @param string $key |
142
|
|
|
* @param mixed $value |
143
|
|
|
* @param int $ttl |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function set($key, $value, $ttl = 0) |
148
|
|
|
{ |
149
|
|
|
$ttl = $this->fromDefaultTtl($ttl); |
150
|
|
|
|
151
|
|
|
if ($ttl >= 0) { |
152
|
|
|
$calculatedTtl = $this->getCalculatedTtl($ttl); |
153
|
|
|
$data = $this->buildDataCache($value, $calculatedTtl); |
154
|
|
|
\file_put_contents($this->getFilenameFromCacheKey($key), $data); |
155
|
|
|
$this->setChain($key, $value, $ttl); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param $ttl |
163
|
|
|
* |
164
|
|
|
* @return int |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
private function getCalculatedTtl($ttl) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$calculatedTtl = \strtotime(\sprintf('now +%s seconds', $ttl)); |
169
|
|
|
|
170
|
|
|
if (0 == $ttl) { |
171
|
|
|
$calculatedTtl = \strtotime('now +10 years'); |
172
|
|
|
} |
173
|
|
|
return $calculatedTtl; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param $value |
178
|
|
|
* @param $calculatedTtl |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
private function buildDataCache($value, $calculatedTtl) |
183
|
|
|
{ |
184
|
|
|
return $this->storageDataStructure( |
185
|
|
|
[ |
186
|
|
|
'value' => $value, |
187
|
|
|
'expires' => new DateTime(\date('Y-m-d H:i:s', $calculatedTtl)) |
188
|
|
|
] |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Delete a value identified by $key. |
194
|
|
|
* |
195
|
|
|
* @param string $key |
196
|
|
|
* |
197
|
|
|
* @throws \RuntimeException |
198
|
|
|
*/ |
199
|
|
|
public function delete($key) |
200
|
|
|
{ |
201
|
|
|
$fileKey = $this->getFilenameFromCacheKey($key); |
202
|
|
|
|
203
|
|
|
if (true === \file_exists($fileKey)) { |
204
|
|
|
$this->removeCacheFile($fileKey); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->deleteChain($key); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Checks the availability of the cache service. |
212
|
|
|
* |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
public function isAvailable() |
216
|
|
|
{ |
217
|
|
|
return \file_exists($this->cacheDir) && \is_dir($this->cacheDir) && \is_writable($this->cacheDir); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Clears all expired values from cache. |
222
|
|
|
* |
223
|
|
|
* @return mixed |
224
|
|
|
*/ |
225
|
|
|
public function clear() |
226
|
|
|
{ |
227
|
|
|
$this->clearCacheFiles($this->cacheDir); |
228
|
|
|
$this->clearChain(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param string $directory |
233
|
|
|
*/ |
234
|
|
|
private function clearCacheFiles($directory) |
235
|
|
|
{ |
236
|
|
|
foreach (\glob("{$directory}/*") as $file) { |
237
|
|
|
if (\is_dir($file)) { |
238
|
|
|
$this->clearCacheFiles($file); |
239
|
|
|
} else { |
240
|
|
|
$value = $this->restoreDataStructure(\file_get_contents($file)); |
241
|
|
|
if ($value['expires'] < (new DateTime())) { |
242
|
|
|
$this->removeCacheFile($file); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Clears all values from the cache. |
250
|
|
|
* |
251
|
|
|
* @return mixed |
252
|
|
|
*/ |
253
|
|
|
public function drop() |
254
|
|
|
{ |
255
|
|
|
$this->removeCacheFiles($this->cacheDir); |
256
|
|
|
$this->dropChain(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param string $directory |
261
|
|
|
*/ |
262
|
|
|
private function removeCacheFiles($directory) |
263
|
|
|
{ |
264
|
|
|
foreach (\glob("{$directory}/*") as $file) { |
265
|
|
|
if (\is_dir($file)) { |
266
|
|
|
$this->removeCacheFiles($file); |
267
|
|
|
} else { |
268
|
|
|
$this->removeCacheFile($file); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.