Skip to content

Commit

Permalink
avoid duplicate requests to the cache proxy. fix #125
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Sep 2, 2014
1 parent 1e0a515 commit ec7540e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.1.x-dev"
}
}
}
5 changes: 4 additions & 1 deletion src/ProxyClient/AbstractProxyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ public function flush()
*/
protected function queueRequest($method, $url, array $headers = array())
{
$this->queue[] = $this->createRequest($method, $url, $headers);
$signature = md5($method . "\n" . $url . "\n" . implode("\n", ksort($headers)));
if (!isset($this->queue[$signature])) {
$this->queue[$signature] = $this->createRequest($method, $url, $headers);
}
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/ProxyClient/VarnishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,41 @@ function ($requests) use ($self) {
);
}

public function testEliminateDuplicates()
{
$self = $this;
$client = \Mockery::mock('\Guzzle\Http\Client[send]', array('', null))
->shouldReceive('send')
->once()
->with(
\Mockery::on(
function ($requests) use ($self) {
/** @type Request[] $requests */
$self->assertCount(4, $requests);
foreach ($requests as $request) {
$self->assertEquals('PURGE', $request->getMethod());
}

return true;
}
)
)
->getMock();

$varnish = new Varnish(array('127.0.0.1', '127.0.0.2'), 'fos.lo', $client);

$this->assertEquals(
2,
$varnish
->purge('/c', array('a' => 'b', 'c' => 'd'))
->purge('/c', array('c' => 'd', 'a' => 'b')) // same request (header order is not significant)
->purge('/c') // different request as headers different
->purge('/c')
->flush()
);
}


protected function setUp()
{
$this->mock = new MockPlugin();
Expand Down

0 comments on commit ec7540e

Please sign in to comment.