diff --git a/examples/Commands/waitaof.php b/examples/Commands/waitaof.php new file mode 100644 index 00000000..c9fd9705 --- /dev/null +++ b/examples/Commands/waitaof.php @@ -0,0 +1,41 @@ +info(); +$enabled = false; + +if ($info['Persistence']['aof_enabled'] === '0') { + $client->config('set', 'appendonly', 'yes'); + $enabled = true; +} + +// 2. Set key value pair +$response = $client->set('foo', 'bar'); +echo "Key-value pair set status: {$response}\n"; + +// 3. Run WAITAOF command to make sure that all previous writes was fsynced +$response = $client->waitaof(1, 0, 0); + +echo "Quantity of local instances that was fsynced - {$response[0]}, quantity of replicas - {$response[1]}"; + +// 4. Disable appendonly mode if it was enabled during script execution +if ($enabled) { + $client->config('set', 'appendonly', 'no'); +} diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index d6b9bb90..a92f4949 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -304,6 +304,7 @@ use Predis\Command\Redis\Container\Search\FTCURSOR; * @method $this exec() * @method $this multi() * @method $this unwatch() + * @method $this waitaof(int $numLocal, int $numReplicas, int $timeout) * @method $this watch($key) * @method $this eval($script, $numkeys, $keyOrArg1 = null, $keyOrArgN = null) * @method $this eval_ro(string $script, array $keys, ...$argument) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 5053c9df..4f95400b 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -322,6 +322,7 @@ use Predis\Response\Status; * @method array|null exec() * @method mixed multi() * @method mixed unwatch() + * @method array waitaof(int $numLocal, int $numReplicas, int $timeout) * @method mixed watch(string $key) * @method mixed eval(string $script, int $numkeys, string ...$keyOrArg = null) * @method mixed eval_ro(string $script, array $keys, ...$argument) diff --git a/src/Command/Redis/WAITAOF.php b/src/Command/Redis/WAITAOF.php new file mode 100644 index 00000000..f58cc054 --- /dev/null +++ b/src/Command/Redis/WAITAOF.php @@ -0,0 +1,29 @@ +getClient(); + $this->assertEquals('OK', $redis->config('set', 'appendonly', 'yes')); + } + + /** + * {@inheritDoc} + */ + protected function getExpectedCommand(): string + { + return WAITAOF::class; + } + + /** + * {@inheritDoc} + */ + protected function getExpectedId(): string + { + return 'WAITAOF'; + } + + /** + * @group disconnected + */ + public function testFilterArguments(): void + { + $actualArguments = $expectedArguments = [1, 2, 3]; + + $command = $this->getCommand(); + $command->setArguments($actualArguments); + + $this->assertSameValues($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $this->assertSame(1, $this->getCommand()->parseResponse(1)); + } + + /** + * @group connected + * @return void + * @requiresRedisVersion >= 7.2.0 + */ + public function testReturnQuantityOfSyncedAOFInstances(): void + { + $redis = $this->getClient(); + + $this->assertEquals('OK', $redis->set('foo', 'bar')); + $this->assertSame([1, 0], $redis->waitaof(1, 0, 0)); + } + + protected function tearDown(): void + { + $redis = $this->getClient(); + $this->assertEquals('OK', $redis->config('set', 'appendonly', 'no')); + } +}