Repository navigation
Expand file tree
/
Copy pathListCacheAdapter.php
More file actions
307 lines (260 loc) · 9.66 KB
/
Copy pathListCacheAdapter.php
File metadata and controls
307 lines (260 loc) · 9.66 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
* @author: Jens Giessmann <jg@handcode.de>
*
* @copyright: 2026 - NOW(), Jens Giessmann
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace handcode\flysystem\adapters;
use League\Flysystem\Config;
use League\Flysystem\DecoratedAdapter;
use League\Flysystem\DirectoryListing;
use League\Flysystem\FileAttributes;
use League\Flysystem\FilesystemAdapter;
use yii\base\InvalidArgumentException;
use yii\caching\Cache;
/**
* This is currently just a POC, and should only be used for (more or less) static FS Backends,
* as it maybe NOT handle changes on FS correctly!
* invalidating caches is just in POC state!
*
* so: Handle with care!
*
* ListCacheAdapter is a flysystem Adapter which cache results for:
* - listContents
* - fileExists
* - directoryExists
* - mimeType
*/
class ListCacheAdapter extends DecoratedAdapter implements FilesystemAdapter
{
public const TYPE_LIST = 'list';
public const TYPE_FILE_SIZE = 'fileSize';
public const TYPE_FILE_MIME = 'mimeType';
public const TYPE_FILE_EXISTS = 'fileExists';
public const TYPE_DIR_EXISTS = 'dirExists';
public const TYPE_MTIME = 'lastModified';
public const TYPE_EXISTS = 'exists';
/**
* @var Cache
*/
public $cache;
/**
* @var string
*/
public $cachePrefix = __CLASS__;
/**
* @var int
*/
public $cacheTTL = 120;
public function __construct(
FilesystemAdapter $adapter,
Cache|callable|string $cache,
callable|string|null $cachePrefix = null,
?int $cacheTTL = null
) {
parent::__construct($adapter);
$this->initCache($cache);
$this->initCachePrefix($cachePrefix);
$this->cacheTTL = $cacheTTL ?: $this->cacheTTL;
}
// ----- caching wrapper for read and metadata operations ------
public function listContents(string $path, bool $deep): iterable
{
\Yii::debug(__FUNCTION__ . ' called for path: ' . $path, __CLASS__);
$cKey = $this->getCacheKey($path, self::TYPE_LIST, [$deep]);
$cData = $this->cache->get($cKey);
if (false !== $cData) {
\Yii::debug('got list from cache for: ' . $path, __CLASS__);
return new DirectoryListing($cData);
}
$list = $this->adapter->listContents($path, $deep);
if ($list instanceof DirectoryListing) {
\Yii::debug(__FUNCTION__ . ' got DirectoryListing for path: ' . $path, __CLASS__);
$list = $list->toArray();
} elseif ($list instanceof \Generator) {
\Yii::debug(__FUNCTION__ . ' got Generator for path: ' . $path, __CLASS__);
$list = iterator_to_array($list);
}
if (is_array($list)) {
\Yii::debug(__FUNCTION__ . ' got Array for path: ' . $path, __CLASS__);
$this->cache->set($cKey, $list, $this->cacheTTL);
}
return new DirectoryListing($list);
}
public function fileExists(string $path): bool
{
\Yii::debug(__FUNCTION__ . ' called for path: ' . $path, __CLASS__);
$cKey = $this->getCacheKey($path, self::TYPE_FILE_EXISTS);
if (($data = $this->cache->get($cKey)) !== false) {
\Yii::debug(__FUNCTION__ . ' got value from cache for: ' . $path, __CLASS__);
return boolval($data);
}
$ret = $this->adapter->fileExists($path);
$this->cache->set($cKey, intval($ret), $this->cacheTTL);
return $ret;
}
public function directoryExists(string $path): bool
{
\Yii::debug(__FUNCTION__ . ' called for path: ' . $path, __CLASS__);
$cKey = $this->getCacheKey($path, self::TYPE_DIR_EXISTS);
if (($data = $this->cache->get($cKey)) !== false) {
\Yii::debug(__FUNCTION__ . ' got value from cache for: ' . $path, __CLASS__);
return boolval($data);
}
$ret = $this->adapter->directoryExists($path);
$this->cache->set($cKey, intval($ret), $this->cacheTTL);
return $ret;
}
public function has(string $path): bool
{
\Yii::debug(__FUNCTION__ . ' called for path: ' . $path, __CLASS__);
$cKey = $this->getCacheKey($path, self::TYPE_DIR_EXISTS);
if (($data = $this->cache->get($cKey)) !== false) {
\Yii::debug(__FUNCTION__ . ' got value from cache for: ' . $path, __CLASS__);
return boolval($data);
}
$ret = $this->adapter->has($path);
$this->cache->set($cKey, intval($ret), $this->cacheTTL);
return $ret;
}
public function mimeType(string $path): FileAttributes
{
\Yii::debug(__FUNCTION__ . ' called for path: ' . $path, __CLASS__);
$cKey = $this->getCacheKey($path, self::TYPE_FILE_MIME);
if ($data = $this->cache->get($cKey)) {
\Yii::debug(__FUNCTION__ . ' got value from cache for: ' . $path, __CLASS__);
return $data;
}
$ret = $this->adapter->mimeType($path);
$this->cache->set($cKey, $ret);
return $ret;
}
public function lastModified(string $path): FileAttributes
{
\Yii::debug(__FUNCTION__ . ' called for path: ' . $path, __CLASS__);
$cKey = $this->getCacheKey($path, self::TYPE_MTIME);
if ($data = $this->cache->get($cKey)) {
\Yii::debug(__FUNCTION__ . ' got value from cache for: ' . $path, __CLASS__);
return $data;
}
$ret = $this->adapter->lastModified($path);
$this->cache->set($cKey, $ret);
return $ret;
}
public function fileSize(string $path): FileAttributes
{
\Yii::debug(__FUNCTION__ . ' called for path: ' . $path, __CLASS__);
$cKey = $this->getCacheKey($path, self::TYPE_FILE_SIZE);
if ($data = $this->cache->get($cKey)) {
\Yii::debug(__FUNCTION__ . ' got value from cache for: ' . $path, __CLASS__);
return $data;
}
$ret = $this->adapter->fileSize($path);
$this->cache->set($cKey, $ret);
return $ret;
}
// ------ wrapper for write operations that should invalidate caches ------
public function write(string $path, string $contents, Config $config): void
{
$this->adapter->write($path, $contents, $config);
$this->invalidateCacheForPath($path);
}
public function writeStream(string $path, $contents, Config $config): void
{
$this->adapter->writeStream($path, $contents, $config);
$this->invalidateCacheForPath($path);
}
public function createDirectory(string $path, Config $config): void
{
$this->adapter->createDirectory($path, $config);
$this->invalidateCacheForPath($path);
}
public function delete(string $path): void
{
$this->adapter->delete($path);
$this->invalidateCacheForPath($path);
}
public function deleteDirectory(string $path): void
{
$this->adapter->deleteDirectory($path);
$this->invalidateCacheForPath($path);
}
public function move(string $source, string $destination, Config $config): void
{
$this->adapter->move($source, $destination, $config);
$this->invalidateCacheForPath($source);
$this->invalidateCacheForPath($destination);
}
public function copy(string $source, string $destination, Config $config): void
{
$this->adapter->copy($source, $destination, $config);
$this->invalidateCacheForPath($source);
$this->invalidateCacheForPath($destination);
}
// ------ internal init and helper methods ------
protected function invalidateCacheForPath(string $path, array $variations = [], $parentCleanup = true)
{
$simpleCacheTypeKeys = [
self::TYPE_FILE_SIZE,
self::TYPE_FILE_MIME,
self::TYPE_FILE_EXISTS,
self::TYPE_DIR_EXISTS,
self::TYPE_EXISTS,
self::TYPE_MTIME,
];
$variationCacheTypeKeys = [
self::TYPE_LIST,
];
foreach ($simpleCacheTypeKeys as $keyType) {
$cKey = $this->getCacheKey($path, $keyType);
$this->cache->delete($cKey);
}
foreach ($variationCacheTypeKeys as $keyType) {
$cKey = $this->getCacheKey($path, $keyType, $variations);
$this->cache->delete($cKey);
}
// invalidate the parent, with special handling for root (empty path)
if (empty($path)) {
return;
}
$p = dirname($path);
if ('.' === $p) {
$p = '';
}
$this->invalidateCacheForPath($p, $variations, false);
}
protected function getCacheKey(string $path, string $type, array $variations = []): string
{
return md5(implode('_', [$this->cachePrefix, $path, $type, implode('_', $variations)]));
}
protected function initCachePrefix($cachePrefix)
{
$this->cachePrefix = $cachePrefix ? (string) self::parseProperty($cachePrefix) : $this->cachePrefix;
}
protected function initCache($cache)
{
$cache = self::parseProperty($cache);
try {
if (is_string($cache)) {
$cache = \Yii::$app->get($cache);
}
} catch (\Exception $e) {
\Yii::error($e->getMessage());
throw new InvalidArgumentException('cache must be valid component');
}
if (!$cache instanceof Cache) {
throw new InvalidArgumentException('cache must be valid cache component');
}
$this->cache = $cache;
}
protected static function parseProperty($prop, $attributes = null)
{
if (is_callable($prop)) {
return call_user_func($prop, $attributes);
}
return $prop;
}
}