Add clear method to pipeline (#749)

* Add clear method to pipeline

Ability to clean pipeline and responses when reusing pipelines.
Allows pipeline to be executed using chunks without out-of-control
increase in memory usage.

* spacing

Co-authored-by: Till Krüss <tillkruss@users.noreply.github.com>
This commit is contained in:
Karol Hrusza
2022-03-11 17:48:10 +01:00
committed by GitHub
parent ab0c46332c
commit 213f00042f
2 changed files with 66 additions and 0 deletions
+13
View File
@@ -225,6 +225,19 @@ class Pipeline implements ClientContextInterface
return $this->responses;
}
/**
* Clear the buffer holding all of the commands and responses.
*
* @return $this
*/
public function clear()
{
$this->responses = array();
$this->pipeline = new \SplQueue();
return $this;
}
/**
* Returns if the pipeline should throw exceptions on server errors.
*
+53
View File
@@ -234,6 +234,59 @@ class PipelineTest extends PredisTestCase
$this->assertSame(array('one', 'two', 'three', 'four'), $pipeline->execute());
}
/**
* @group disconnected
*/
public function testClearBuffer()
{
$connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
$connection->expects($this->never())
->method('writeRequest');
$connection->expects($this->never())
->method('readResponse')
->will($this->returnCallback($this->getReadCallback()));
$pipeline = new Pipeline(new Client($connection));
$pipeline->echo('one');
$pipeline->echo('two');
$pipeline->clear();
$this->assertSame(array(), $pipeline->execute());
}
/**
* @group disconnected
*/
public function testClearResponses()
{
$connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
$connection->expects($this->exactly(4))
->method('writeRequest');
$connection->expects($this->exactly(4))
->method('readResponse')
->will($this->returnCallback($this->getReadCallback()));
$pipeline = new Pipeline(new Client($connection));
$pipeline->echo('one');
$pipeline->echo('two');
$this->assertSame(array('one', 'two'), $pipeline->execute());
$pipeline->clear();
$pipeline->echo('three');
$pipeline->echo('four');
$this->assertSame(array('three', 'four'), $pipeline->execute());
$pipeline->clear();
$this->assertSame(array(), $pipeline->execute());
}
/**
* @group disconnected
*/