1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RemotelyLiving\PHPCacheAdapter\SimpleCache; |
6
|
|
|
|
7
|
|
|
use Psr\SimpleCache; |
8
|
|
|
|
9
|
|
|
final class Memory extends AbstractAdapter |
10
|
|
|
{ |
11
|
|
|
private array $cache = []; |
12
|
|
|
|
13
|
|
|
private ?int $maxItems; |
14
|
|
|
|
15
|
|
|
private function __construct(int $maxItems = null) |
16
|
|
|
{ |
17
|
|
|
$this->maxItems = $maxItems; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public static function create(int $maxItems = null): SimpleCache\CacheInterface |
21
|
|
|
{ |
22
|
|
|
return new self($maxItems); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function flush(): bool |
26
|
|
|
{ |
27
|
|
|
$this->cache = []; |
28
|
|
|
return empty($this->cache); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function exists(string $key): bool |
32
|
|
|
{ |
33
|
|
|
$this->expireEntries(); |
34
|
|
|
return isset($this->cache[$key]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
|
|
protected function multiGet(array $keys, $default = null): \Generator |
41
|
|
|
{ |
42
|
|
|
$this->expireEntries(); |
43
|
|
|
foreach ($keys as $key) { |
44
|
|
|
yield $key => $this->cache[$key]['value'] ?? $default; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function multiDelete(array $keys): bool |
49
|
|
|
{ |
50
|
|
|
$this->expireEntries(); |
51
|
|
|
foreach ($keys as $key) { |
52
|
|
|
unset($this->cache[$key]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function multiSave(array $values, int $ttl = null): bool |
59
|
|
|
{ |
60
|
|
|
$this->expireEntries(); |
61
|
|
|
$ttl = $ttl ?? $this->getDefaultTTLSeconds(); |
62
|
|
|
foreach ($values as $key => $value) { |
63
|
|
|
$this->cache[$key]['value'] = $value; |
64
|
|
|
$this->cache[$key]['expiresAt'] = $this->getExpiresFromTTL($ttl); |
65
|
|
|
if ($this->isAtEvictionThreshold()) { |
66
|
|
|
$this->evict(); |
67
|
|
|
; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function timestamp(): int |
75
|
|
|
{ |
76
|
|
|
return time(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function expireEntries(): void |
80
|
|
|
{ |
81
|
|
|
$time = $this->timestamp(); |
82
|
|
|
foreach ($this->cache as $key => $entry) { |
83
|
|
|
if ($entry['expiresAt'] === null || $entry['expiresAt'] > $time) { |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
unset($this->cache[$key]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getExpiresFromTTL(?int $ttl): ?int |
92
|
|
|
{ |
93
|
|
|
if ($ttl === null) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->timestamp() + $ttl; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function isAtEvictionThreshold(): bool |
101
|
|
|
{ |
102
|
|
|
if ($this->maxItems === null) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return (count($this->cache) > $this->maxItems); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function evict(): void |
110
|
|
|
{ |
111
|
|
|
array_shift($this->cache); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|