-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRedisCluster.php
More file actions
154 lines (129 loc) · 3.78 KB
/
RedisCluster.php
File metadata and controls
154 lines (129 loc) · 3.78 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit;
class RedisCluster extends TimeLimit
{
public const NAMESPACE = 'abuse';
/**
* @var \RedisCluster
*/
protected \RedisCluster $redis;
/**
* @var int
*/
protected int $ttl;
public function __construct(string $key, int $limit, int $seconds, \RedisCluster $redis)
{
$this->redis = $redis;
$this->key = $key;
$this->ttl = $seconds;
$now = \time();
$this->timestamp = (int)($now - ($now % $seconds));
$this->limit = $limit;
}
/**
* Get count for a key at specific timestamp
*
* @param string $key
* @param int $timestamp
* @return integer
*/
protected function count(string $key, int $timestamp): int
{
if (0 == $this->limit) { // No limit no point for counting
return 0;
}
if (! \is_null($this->count)) { // Get fetched result
return $this->count;
}
/** @var string|false $count */
$count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $timestamp);
if ($count === false) {
$this->count = 0;
} else {
$this->count = intval($count);
}
return $this->count;
}
/**
* Record a hit for a key at specific timestamp
*
* @param string $key
* @param int $timestamp
* @return void
*/
protected function hit(string $key, int $timestamp): void
{
if (0 == $this->limit) { // No limit no point for counting
return;
}
$key = self::NAMESPACE . '__'. $key .'__'. $timestamp;
$this->redis->multi();
$this->redis->incr($key);
$this->redis->expire($key, $this->ttl);
$this->redis->exec();
$this->count = ($this->count ?? 0) + 1;
}
/**
* Set count for a key at specific timestamp
*
* @param string $key
* @param int $timestamp
* @param int $value
* @return void
*/
protected function set(string $key, int $timestamp, int $value): void
{
$key = self::NAMESPACE . '__' . $key . '__' . $timestamp;
$this->redis->multi();
$this->redis->set($key, (string)$value);
$this->redis->expire($key, $this->ttl);
$this->redis->exec();
$this->count = $value;
}
/**
* Get abuse logs with proper cursor-based pagination
*
* @param int|null $offset
* @param int|null $limit
* @return array<string, mixed>
*/
public function getLogs(?int $offset = 0, ?int $limit = 25): array
{
$offset = $offset ?? 0;
$limit = $limit ?? 25;
$matches = [];
$pattern = self::NAMESPACE . '__*';
// Get all keys from each master
foreach ($this->redis->_masters() as $master) {
$cursor = null;
do {
/** @phpstan-ignore-next-line */
$keys = $this->redis->scan($cursor, $master, $pattern, 100);
if ($keys !== false) {
$matches = array_merge($matches, $keys);
}
} while ($cursor > 0 && count($matches) < $offset + $limit);
}
// Sort to ensure consistent ordering
sort($matches);
// Apply offset and limit
$matches = array_slice($matches, $offset, $limit);
if (empty($matches)) {
return [];
}
// Batch fetch values using mget
$values = $this->redis->mget($matches);
return array_combine($matches, $values);
}
/**
* No need for manual cleanup - using Redis TTL
*
* @param int $timestamp
* @return bool
*/
public function cleanup(int $timestamp): bool
{
return true;
}
}