mirror of
https://github.com/predis/predis.git
synced 2026-09-15 21:07:08 +00:00
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:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user