-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryKvOps.php
More file actions
125 lines (110 loc) · 3.38 KB
/
Copy pathInMemoryKvOps.php
File metadata and controls
125 lines (110 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace Ephpm\Cache\Symfony;
/**
* In-process backend used by the test suite and by anyone who wants to
* exercise the adapter without the ePHPm runtime present. Stores
* everything in PHP arrays — explicitly NOT a production cache.
*
* TTL semantics match the SAPI: TTLs are stored as monotonic deadlines
* in milliseconds; lazy expiry (a key is only "gone" when it's looked up
* after its deadline). No background sweeper.
*/
final class InMemoryKvOps implements KvOpsInterface
{
/** @var array<string, string> */
private array $values = [];
/** @var array<string, int> deadline in milliseconds since epoch */
private array $deadlines = [];
public function get(string $key): ?string
{
return $this->liveValue($key);
}
public function set(string $key, string $value, int $ttlSeconds = 0): bool
{
$this->values[$key] = $value;
if ($ttlSeconds > 0) {
$this->deadlines[$key] = $this->nowMs() + ($ttlSeconds * 1000);
} else {
unset($this->deadlines[$key]);
}
return true;
}
public function del(string $key): int
{
if ($this->liveValue($key) === null) {
return 0;
}
unset($this->values[$key], $this->deadlines[$key]);
return 1;
}
public function exists(string $key): bool
{
return $this->liveValue($key) !== null;
}
public function incrBy(string $key, int $delta): int
{
$current = $this->liveValue($key);
if ($current === null) {
$next = $delta;
} elseif (\preg_match('/^-?\d+$/', $current) === 1) {
$next = ((int) $current) + $delta;
} else {
throw new \RuntimeException("value at key '{$key}' is not an integer");
}
$this->values[$key] = (string) $next;
// INCR preserves any existing deadline.
return $next;
}
public function expire(string $key, int $ttlSeconds): bool
{
if ($this->liveValue($key) === null || $ttlSeconds <= 0) {
return false;
}
$this->deadlines[$key] = $this->nowMs() + ($ttlSeconds * 1000);
return true;
}
public function ttl(string $key): int
{
$ms = $this->pttl($key);
if ($ms < 0) {
return $ms;
}
// Round up so 1..999 ms = 1 s, matching the SAPI's ttl().
return (int) (($ms + 999) / 1000);
}
public function pttl(string $key): int
{
if ($this->liveValue($key) === null) {
return -2;
}
if (!isset($this->deadlines[$key])) {
return -1;
}
return \max(0, $this->deadlines[$key] - $this->nowMs());
}
public function flush(): bool
{
$this->values = [];
$this->deadlines = [];
return true;
}
/**
* Resolve a key's value, expiring it lazily if its deadline has passed.
*/
private function liveValue(string $key): ?string
{
if (!\array_key_exists($key, $this->values)) {
return null;
}
if (isset($this->deadlines[$key]) && $this->deadlines[$key] <= $this->nowMs()) {
unset($this->values[$key], $this->deadlines[$key]);
return null;
}
return $this->values[$key];
}
private function nowMs(): int
{
return (int) (\microtime(true) * 1000);
}
}