-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientManager.php
More file actions
129 lines (104 loc) · 3.51 KB
/
Copy pathClientManager.php
File metadata and controls
129 lines (104 loc) · 3.51 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
126
127
128
129
<?php
// SPDX-License-Identifier: BSD-3-Clause
declare(strict_types=1);
namespace SingleA\Bundles\Redis;
use Psr\Log\LoggerInterface;
use SingleA\Contracts\Persistence\ClientManagerInterface;
final class ClientManager implements ClientManagerInterface
{
public function __construct(
private readonly string $key,
private readonly \Predis\ClientInterface|\Redis|\RedisCluster $redis,
private readonly ?LoggerInterface $logger = null,
) {}
public function exists(string $id, bool $touch = true): bool
{
$exists = (bool) $this->redis->hexists($this->key, $id);
if ($exists && $touch) {
$this->touch($id);
}
return $exists;
}
public function touch(string $id): void
{
try {
$this->redis->hset($this->key, $id, (string) time());
} finally {
$this->logger?->debug('Key "'.$this->key.'": client '.$id.' touched.');
}
}
public function getLastAccess(string $id): \DateTimeImmutable
{
$timestamp = $this->redis->hget($this->key, $id);
if (!is_numeric($timestamp)) {
throw new \InvalidArgumentException('Unknown id specified: '.$id);
}
return new \DateTimeImmutable("@{$timestamp}");
}
public function findInactiveSince(\DateTimeInterface $datetime): iterable
{
$inactiveIds = null;
$error = null;
try {
/** @psalm-suppress MixedAssignment */
$inactiveIds = $this->redis->eval(
<<<'LUA'
local last_access = redis.call('hgetall', KEYS[1])
local ids = {}
for i = 1, #last_access, 2 do
if last_access[i + 1] <= KEYS[2] then
ids[#ids + 1] = last_access[i]
end
end
return ids
LUA,
[
$this->key,
$datetime->getTimestamp(),
],
2,
);
} catch (\Throwable $exception) {
$error = $exception->getMessage();
}
if (\is_array($inactiveIds)) {
/** @psalm-suppress MixedArgument */
return array_map('strval', $inactiveIds);
}
$error ??= $this->getRedisLastError();
if (\is_string($error)) {
$this->logger?->error('Error during seek of inactive clients ids: '.$error);
}
return [];
}
public function findOldest(): ?string
{
$ids = $this->redis->hkeys($this->key);
if (!\is_array($ids) || empty($ids)) {
return null;
}
sort($ids);
return (string) reset($ids);
}
public function remove(string ...$ids): int
{
if (empty($ids)) {
return 0;
}
try {
/** @psalm-suppress InvalidArgument, InvalidCast */
return (int) $this->redis->hdel($this->key, ...$ids); // @phan-suppress-current-line PhanParamTooManyUnpack, PhanTypeMismatchArgumentProbablyReal
} finally {
$this->logger?->debug('Key "'.$this->key.'": clients removed: '.implode(', ', $ids).'.');
}
}
private function getRedisLastError(): ?string
{
$error = null;
if ($this->redis instanceof \Redis || $this->redis instanceof \RedisCluster) {
$error = $this->redis->getLastError();
$this->redis->clearLastError();
}
return $error;
}
}