-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPoolRedis.php
More file actions
88 lines (78 loc) · 2.33 KB
/
PoolRedis.php
File metadata and controls
88 lines (78 loc) · 2.33 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
<?php
namespace Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Pools\Pool as UtopiaPool;
use Utopia\Abuse\Adapters\TimeLimit\Redis as RedisAdapter;
use Redis;
class PoolRedis extends RedisAdapter
{
/**
* @var UtopiaPool<covariant Redis>
*/
protected UtopiaPool $pool;
/**
* @param string $key
* @param int $limit
* @param int $seconds
* @param UtopiaPool<covariant Redis> $pool The pool to use for connections. Must contain instances of TimeLimit.
*
* @throws \Exception
*/
public function __construct(string $key, int $limit, int $seconds, UtopiaPool $pool)
{
$this->pool = $pool;
$this->key = $key;
$this->limit = $limit;
$this->ttl = $seconds;
$now = \time();
$this->timestamp = (int)($now - ($now % $seconds));
$this->pool->use(function (mixed $resource) {
if (! ($resource instanceof Redis)) {
throw new \Exception('Pool must contain instances of '.Redis::class);
}
});
}
/**
* Forward method calls to the internal adapter instance via the pool.
*
* Required because __call() can't be used to implement abstract methods.
*
* @param string $method
* @param array<mixed> $args
* @return mixed
*/
public function delegate(string $method, array $args): mixed
{
return $this->pool->use(function (Redis $redis) use ($method, $args) {
$this->redis = $redis;
return parent::{$method}(...$args);
});
}
protected function count(string $key, int $timestamp): int
{
/**
* @var int $result
*/
$result = $this->delegate(__FUNCTION__, \func_get_args());
return $result;
}
protected function hit(string $key, int $timestamp): void
{
$this->delegate(__FUNCTION__, \func_get_args());
}
public function cleanup(int $timestamp): bool
{
/**
* @var bool $result
*/
$result = $this->delegate(__FUNCTION__, \func_get_args());
return $result;
}
public function getLogs(?int $offset = null, ?int $limit = 25): array
{
/**
* @var array<string, mixed> $result
*/
$result = $this->delegate(__FUNCTION__, \func_get_args());
return $result;
}
}