1
|
|
|
<?php |
|
|
|
|
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Linio\Component\Cache\Adapter; |
5
|
|
|
|
6
|
|
|
use Linio\Component\Cache\Exception\KeyNotFoundException; |
7
|
|
|
|
8
|
|
|
class ApcuAdapter extends AbstractAdapter implements AdapterInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var int |
12
|
|
|
*/ |
13
|
|
|
protected $ttl; |
14
|
|
|
|
15
|
|
|
public function __construct(array $config = []) |
16
|
|
|
{ |
17
|
|
|
// default config |
18
|
|
|
$this->ttl = 0; |
19
|
|
|
|
20
|
|
|
// config |
21
|
|
|
if (isset($config['ttl'])) { |
22
|
|
|
$this->ttl = $config['ttl']; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if (isset($config['cache_not_found_keys'])) { |
26
|
|
|
$this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys']; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function get(string $key) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$value = apcu_fetch($this->addNamespaceToKey($key), $success); |
|
|
|
|
33
|
|
|
|
34
|
|
|
if (!$success) { |
35
|
|
|
throw new KeyNotFoundException(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $value; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getMulti(array $keys): array |
42
|
|
|
{ |
43
|
|
|
$values = []; |
44
|
|
|
|
45
|
|
|
foreach ($keys as $key) { |
46
|
|
|
$value = apcu_fetch($this->addNamespaceToKey($key), $success); |
|
|
|
|
47
|
|
|
if ($success) { |
48
|
|
|
$values[$key] = $value; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $values; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function set(string $key, $value): bool |
56
|
|
|
{ |
57
|
|
|
return apcu_add($this->addNamespaceToKey($key), $value, $this->ttl); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function setMulti(array $data): bool |
61
|
|
|
{ |
62
|
|
|
$namespacedData = $this->addNamespaceToKeys($data, true); |
63
|
|
|
$errors = apcu_add($namespacedData, $this->ttl); |
64
|
|
|
|
65
|
|
|
return empty($errors); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function contains(string $key): bool |
69
|
|
|
{ |
70
|
|
|
return apcu_exists($this->addNamespaceToKey($key)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function delete(string $key): bool |
74
|
|
|
{ |
75
|
|
|
apcu_delete($this->addNamespaceToKey($key)); |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function deleteMulti(array $keys): bool |
81
|
|
|
{ |
82
|
|
|
foreach ($keys as $key) { |
83
|
|
|
apcu_delete($this->addNamespaceToKey($key)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function flush(): bool |
90
|
|
|
{ |
91
|
|
|
return apcu_clear_cache(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.