1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
4
|
|
|
|
5
|
|
|
trait Streams |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Acknowledge one or more pending messages. |
9
|
|
|
* |
10
|
|
|
* @param string $stream |
11
|
|
|
* @param string $group |
12
|
|
|
* @param array $messages |
13
|
|
|
* |
14
|
|
|
* @return int The number of messages Redis reports as acknowledged. |
15
|
|
|
*/ |
16
|
|
|
public function xAck(string $stream, string $group, array $messages): int |
17
|
|
|
{ |
18
|
|
|
return $this->redis->xAck($stream, $group, $messages); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Appends the specified stream entry to the stream at the specified key. |
24
|
|
|
* If the key does not exist, as a side effect of running this command the |
25
|
|
|
* key is created with a stream value. |
26
|
|
|
* See: https://redis.io/commands/xadd. |
27
|
|
|
* |
28
|
|
|
* @param string $key |
29
|
|
|
* @param string $id |
30
|
|
|
* @param array $message |
31
|
|
|
* @param int|integer $maxLenght |
32
|
|
|
* @param bool $approximate |
33
|
|
|
* |
34
|
|
|
* @return string The added message ID |
35
|
|
|
*/ |
36
|
|
|
public function xAdd( |
37
|
|
|
string $key, |
38
|
|
|
string $id, |
39
|
|
|
array $message, |
40
|
|
|
?int $maxLenght = null, |
41
|
|
|
?bool $approximate = null |
42
|
|
|
): string { |
43
|
|
|
if (is_null($maxLenght) && is_null($approximate)) { |
44
|
|
|
return $this->redis->xAdd($key, $id, $message); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (is_null($approximate)) { |
48
|
|
|
return $this->redis->xAdd($key, $id, $message, $maxLenght); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->redis->xAdd($key, $id, $message, $maxLenght, $approximate); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Claim ownership of one or more pending messages. |
57
|
|
|
* |
58
|
|
|
* @param string $stream |
59
|
|
|
* @param string $group |
60
|
|
|
* @param string $consumer |
61
|
|
|
* @param int $minIdleTime |
62
|
|
|
* @param array $messageIds |
63
|
|
|
* @param array|null $options |
64
|
|
|
* |
65
|
|
|
* @return array Either an array of message IDs along with |
66
|
|
|
* corresponding data, or just an array of |
67
|
|
|
* IDs (if the 'JUSTID' option was passed). |
68
|
|
|
*/ |
69
|
|
|
public function xClaim( |
70
|
|
|
string $stream, |
71
|
|
|
string $group, |
72
|
|
|
string $consumer, |
73
|
|
|
int $minIdleTime, |
74
|
|
|
array $messageIds, |
75
|
|
|
?array $options = null |
76
|
|
|
): array { |
77
|
|
|
if (!is_null($options) && !$this->_checkClaimOptions($options)) { |
78
|
|
|
throw new \Exception("Bad Claim Options", 1); |
79
|
|
|
} |
80
|
|
|
if (is_null($options)) { |
81
|
|
|
return $this->redis->xClaim($stream, $group, $consumer, $minIdleTime, $messageIds); |
82
|
|
|
} else { |
83
|
|
|
return $this->redis->xClaim($stream, $group, $consumer, $minIdleTime, $messageIds, $options); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Delete one or more messages from a stream. |
90
|
|
|
* |
91
|
|
|
* @param string $stream |
92
|
|
|
* @param array $messageIds |
93
|
|
|
* |
94
|
|
|
* @return int The number of messages removed. |
95
|
|
|
*/ |
96
|
|
|
public function xDel(string $stream, array $messageIds): int |
97
|
|
|
{ |
98
|
|
|
return $this->redis->xDel($stream, $messageIds); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* This command is used in order to create, destroy, or manage consumer groups. |
104
|
|
|
* |
105
|
|
|
* @param string $command [description] |
106
|
|
|
* @param string|null $stream [description] |
107
|
|
|
* @param string|null $group [description] |
108
|
|
|
* @param [type] $messageId_consumerName [description] |
|
|
|
|
109
|
|
|
* @param bool|boolean $makeStream [description] |
110
|
|
|
* |
111
|
|
|
* @return mixed This command returns different |
112
|
|
|
* types depending on the specific |
113
|
|
|
* XGROUP command executed. |
114
|
|
|
*/ |
115
|
|
|
public function xGroup( |
116
|
|
|
string $command, |
117
|
|
|
?string $stream = null, |
118
|
|
|
?string $group = null, |
119
|
|
|
$messageId_consumerName = null, |
120
|
|
|
bool $makeStream = false |
121
|
|
|
) { |
122
|
|
|
return $this->redis->xGroup($command, $stream, $group, $messageId_consumerName, $makeStream); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function xInfo(): bool |
126
|
|
|
{ |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function xLen(): bool |
131
|
|
|
{ |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function xPending(): bool |
136
|
|
|
{ |
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function xRange(): bool |
141
|
|
|
{ |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function xRead(): bool |
146
|
|
|
{ |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function xReadGroup(): bool |
151
|
|
|
{ |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function xRevRange(): bool |
156
|
|
|
{ |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function xTrim(): bool |
161
|
|
|
{ |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* ************************************************************************ |
167
|
|
|
* H E L P E R F U N C T I O N S |
168
|
|
|
* ************************************************************************ |
169
|
|
|
*/ |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Claim Options available |
173
|
|
|
* |
174
|
|
|
* Note: 'TIME', and 'IDLE' are mutually exclusive |
175
|
|
|
* |
176
|
|
|
* 'IDLE' => $value, Set the idle time to $value ms |
177
|
|
|
* 'TIME' => $value, Set the idle time to now - $value |
178
|
|
|
* 'RETRYCOUNT' => $value, Update message retrycount to $value |
179
|
|
|
* 'FORCE', Claim the message(s) even if they're not pending anywhere |
180
|
|
|
* 'JUSTID',Instruct Redis to only return IDs |
181
|
|
|
* |
182
|
|
|
* @param array $options |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
private function _checkClaimOptions(array $options): bool |
187
|
|
|
{ |
188
|
|
|
$available = ['IDLE', 'TIME', 'RETRYCOUNT', 'FORCE', 'JUSTID']; |
189
|
|
|
foreach ($options as $key => $value) { |
190
|
|
|
$check = is_numeric($key) ? $value : $key; |
191
|
|
|
if (!in_array($check, $available)) { |
192
|
|
|
return false; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
return !(array_key_exists('IDLE', $options) && array_key_exists('TIME', $options)); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: