Skip to content

Commit ec7540e

Browse files
committed
avoid duplicate requests to the cache proxy. fix #125
1 parent 1e0a515 commit ec7540e

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
},
4343
"extra": {
4444
"branch-alias": {
45-
"dev-master": "1.0.x-dev"
45+
"dev-master": "1.1.x-dev"
4646
}
4747
}
4848
}

src/ProxyClient/AbstractProxyClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ public function flush()
130130
*/
131131
protected function queueRequest($method, $url, array $headers = array())
132132
{
133-
$this->queue[] = $this->createRequest($method, $url, $headers);
133+
$signature = md5($method . "\n" . $url . "\n" . implode("\n", ksort($headers)));
134+
if (!isset($this->queue[$signature])) {
135+
$this->queue[$signature] = $this->createRequest($method, $url, $headers);
136+
}
134137
}
135138

136139
/**

tests/Unit/ProxyClient/VarnishTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,41 @@ function ($requests) use ($self) {
351351
);
352352
}
353353

354+
public function testEliminateDuplicates()
355+
{
356+
$self = $this;
357+
$client = \Mockery::mock('\Guzzle\Http\Client[send]', array('', null))
358+
->shouldReceive('send')
359+
->once()
360+
->with(
361+
\Mockery::on(
362+
function ($requests) use ($self) {
363+
/** @type Request[] $requests */
364+
$self->assertCount(4, $requests);
365+
foreach ($requests as $request) {
366+
$self->assertEquals('PURGE', $request->getMethod());
367+
}
368+
369+
return true;
370+
}
371+
)
372+
)
373+
->getMock();
374+
375+
$varnish = new Varnish(array('127.0.0.1', '127.0.0.2'), 'fos.lo', $client);
376+
377+
$this->assertEquals(
378+
2,
379+
$varnish
380+
->purge('/c', array('a' => 'b', 'c' => 'd'))
381+
->purge('/c', array('c' => 'd', 'a' => 'b')) // same request (header order is not significant)
382+
->purge('/c') // different request as headers different
383+
->purge('/c')
384+
->flush()
385+
);
386+
}
387+
388+
354389
protected function setUp()
355390
{
356391
$this->mock = new MockPlugin();

0 commit comments

Comments
 (0)