|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Linio\Component\Cache\Adapter; |
|
6
|
|
|
|
|
7
|
|
|
use Linio\Component\Cache\Exception\KeyNotFoundException; |
|
8
|
|
|
|
|
9
|
|
|
class ArrayAdapter extends AbstractAdapter implements AdapterInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected array $cacheData = []; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
public function __construct(array $config = []) |
|
14
|
|
|
{ |
|
15
|
|
|
if (isset($config['cache_not_found_keys'])) { |
|
16
|
|
|
$this->cacheNotFoundKeys = (bool) $config['cache_not_found_keys']; |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return mixed |
|
22
|
|
|
*/ |
|
23
|
|
|
public function get(string $key) |
|
24
|
|
|
{ |
|
25
|
|
|
if (!array_key_exists($this->addNamespaceToKey($key), $this->cacheData)) { |
|
26
|
|
|
throw new KeyNotFoundException(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $this->cacheData[$this->addNamespaceToKey($key)]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getMulti(array $keys): array |
|
33
|
|
|
{ |
|
34
|
|
|
$values = []; |
|
35
|
|
|
|
|
36
|
|
|
foreach ($keys as $key) { |
|
37
|
|
|
if (array_key_exists($this->addNamespaceToKey($key), $this->cacheData)) { |
|
38
|
|
|
$values[$key] = $this->cacheData[$this->addNamespaceToKey($key)]; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $values; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param mixed $value |
|
47
|
|
|
*/ |
|
48
|
|
|
public function set(string $key, $value): bool |
|
49
|
|
|
{ |
|
50
|
|
|
$this->cacheData[$this->addNamespaceToKey($key)] = $value; |
|
51
|
|
|
|
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function setMulti(array $data): bool |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($data as $key => $value) { |
|
58
|
|
|
$this->cacheData[$this->addNamespaceToKey($key)] = $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function contains(string $key): bool |
|
65
|
|
|
{ |
|
66
|
|
|
return array_key_exists($this->addNamespaceToKey($key), $this->cacheData); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function delete(string $key): bool |
|
70
|
|
|
{ |
|
71
|
|
|
unset($this->cacheData[$this->addNamespaceToKey($key)]); |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function deleteMulti(array $keys): bool |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($keys as $key) { |
|
79
|
|
|
unset($this->cacheData[$this->addNamespaceToKey($key)]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function flush(): bool |
|
86
|
|
|
{ |
|
87
|
|
|
$this->cacheData = []; |
|
88
|
|
|
|
|
89
|
|
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|