|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Audiens\AdForm\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\Adapter\Local; |
|
6
|
|
|
use League\Flysystem\Filesystem; |
|
7
|
|
|
|
|
8
|
|
|
class FileCache extends BaseCache implements CacheInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var Filesystem */ |
|
11
|
|
|
private $filesystem; |
|
12
|
|
|
|
|
13
|
|
|
/** @var int */ |
|
14
|
|
|
private $ttl; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(string $path, int $ttl = 3600) |
|
17
|
|
|
{ |
|
18
|
|
|
parent::__construct($path); |
|
19
|
|
|
|
|
20
|
|
|
if (!\in_array($path[0], ['.', '/'])) { |
|
21
|
|
|
$path = sys_get_temp_dir().DIRECTORY_SEPARATOR.$path; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$adapter = new Local($path); |
|
25
|
|
|
$this->filesystem = new Filesystem($adapter); |
|
26
|
|
|
|
|
27
|
|
|
$this->ttl = $ttl; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getFilesystem(): Filesystem |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->filesystem; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function put(string $providerPrefix, string $uri, array $query, string $data): bool |
|
36
|
|
|
{ |
|
37
|
|
|
$hash = $this->getHash($providerPrefix, $uri, $query); |
|
38
|
|
|
|
|
39
|
|
|
return $this->filesystem->put($hash, $data); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function get(string $providerPrefix, string $uri, array $query) |
|
48
|
|
|
{ |
|
49
|
|
|
$hash = $this->getHash($providerPrefix, $uri, $query); |
|
50
|
|
|
$ttlCutoff = time() - $this->ttl; |
|
51
|
|
|
|
|
52
|
|
|
if ($this->filesystem->has($hash) && $this->filesystem->getTimestamp($hash) > $ttlCutoff) { |
|
53
|
|
|
$data = $this->filesystem->read($hash); |
|
54
|
|
|
|
|
55
|
|
|
if (!empty($data)) { |
|
56
|
|
|
return $data; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function delete(string $providerPrefix, string $uri, array $query): bool |
|
69
|
|
|
{ |
|
70
|
|
|
$hash = $this->getHash($providerPrefix, $uri, $query); |
|
71
|
|
|
|
|
72
|
|
|
if ($this->filesystem->has($hash)) { |
|
73
|
|
|
return $this->filesystem->delete($hash); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function invalidate(string $providerPrefix): bool |
|
85
|
|
|
{ |
|
86
|
|
|
$pattern = $this->prefix.strtolower($providerPrefix).'_'; |
|
87
|
|
|
|
|
88
|
|
|
$files = $this->filesystem->listContents(); |
|
89
|
|
|
foreach ($files as $file) { |
|
90
|
|
|
if (stripos($file['basename'], $pattern) === 0) { |
|
91
|
|
|
$this->filesystem->delete($file['basename']); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|