1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* The Imaging Source Download System PHP Wrapper |
6
|
|
|
* |
7
|
|
|
* PHP wrapper for The Imaging Source Download System Web API. Authored and supported by The Imaging Source Europe GmbH. |
8
|
|
|
* |
9
|
|
|
* @link http://dl-gui.theimagingsource.com to learn more about The Imaging Source Download System |
10
|
|
|
* @link https://github.com/jonathanmaron/theimagingsource-tisd-sdk for the canonical source repository |
11
|
|
|
* @license https://github.com/jonathanmaron/theimagingsource-tisd-sdk/blob/master/LICENSE.md |
12
|
|
|
* @copyright © 2022 The Imaging Source Europe GmbH |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Tisd\Sdk\Cache; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Cache |
19
|
|
|
* |
20
|
|
|
* @package Tisd\Sdk\Cache |
21
|
|
|
*/ |
22
|
|
|
class Cache extends AbstractCache |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Write data to the cache file |
26
|
|
|
* |
27
|
|
|
* @param string $cacheId |
28
|
|
|
* @param array $data |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
9 |
|
public function write(string $cacheId, array $data): bool |
33
|
|
|
{ |
34
|
9 |
|
$filename = $this->getFilename($cacheId); |
35
|
|
|
|
36
|
9 |
|
if (is_file($filename)) { |
37
|
2 |
|
unlink($filename); |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
$buffer = sprintf("<?php\n\nreturn %s;\n", var_export($data, true)); |
41
|
|
|
|
42
|
9 |
|
return is_int(file_put_contents($filename, $buffer)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Read data from the cache file |
47
|
|
|
* |
48
|
|
|
* @param string $cacheId |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
40 |
|
public function read(string $cacheId): array |
53
|
|
|
{ |
54
|
40 |
|
$ret = []; |
55
|
|
|
|
56
|
40 |
|
$filename = $this->getFilename($cacheId); |
57
|
|
|
|
58
|
40 |
|
if (is_readable($filename)) { |
59
|
35 |
|
$timestamp = filemtime($filename); |
60
|
35 |
|
assert(is_int($timestamp)); |
61
|
35 |
|
if ($timestamp + $this->getTtl() > time()) { |
62
|
34 |
|
$ret = (array) include $filename; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
40 |
|
return $ret; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Purge the cache file |
71
|
|
|
* |
72
|
|
|
* @param string $user |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
3 |
|
public function purge(string $user = ''): bool |
77
|
|
|
{ |
78
|
3 |
|
$ret = false; |
79
|
|
|
|
80
|
3 |
|
if ('' === $user) { |
81
|
3 |
|
$user = $this->getUser(); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
$filenames = glob($this->getFilename('*', $user)); |
85
|
3 |
|
assert(is_array($filenames)); |
86
|
3 |
|
foreach ($filenames as $filename) { |
87
|
3 |
|
$ret = unlink($filename); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return $ret; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|