Skip to content

Commit 99a6717

Browse files
committed
have flush return the count of successful invalidation requests
1 parent ff54de7 commit 99a6717

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

src/CacheInvalidator.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ public function invalidateTags(array $tags)
282282
/**
283283
* Send all pending invalidation requests.
284284
*
285-
* @throws ExceptionCollection
285+
* @return int The number of cache invalidations performed per caching server.
286286
*
287-
* @return $this
287+
* @throws ExceptionCollection If any errors occurred during flush.
288288
*/
289289
public function flush()
290290
{
291291
try {
292-
$this->cache->flush();
292+
return $this->cache->flush();
293293
} catch (ExceptionCollection $exceptions) {
294294
foreach ($exceptions as $exception) {
295295
$event = new Event();
@@ -303,7 +303,5 @@ public function flush()
303303

304304
throw $exceptions;
305305
}
306-
307-
return $this;
308306
}
309307
}

src/Invalidation/AbstractCacheProxy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ public function flush()
106106
{
107107
$queue = $this->queue;
108108
if (0 === count($queue)) {
109-
return;
109+
return 0;
110110
}
111111

112112
$this->queue = array();
113113
$this->sendRequests($queue);
114+
115+
return count($queue);
114116
}
115117

116118
/**

src/Invalidation/CacheProxyInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ interface CacheProxyInterface
1212
/**
1313
* Send all pending invalidation requests.
1414
*
15-
* @return $this
15+
* @return int The number of cache invalidations performed per caching server.
1616
*
17-
* @throws ExceptionCollection If any errors occurred during flush
17+
* @throws ExceptionCollection If any errors occurred during flush.
1818
*/
1919
public function flush();
2020
}

tests/Functional/Fixtures/varnish/purge.vcl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ sub vcl_recv {
1010
sub vcl_hit {
1111
if (req.request == "PURGE") {
1212
purge;
13-
error 200 "Purged";
13+
error 204 "Purged";
1414
}
1515
}
1616

1717
sub vcl_miss {
1818
if (req.request == "PURGE") {
1919
purge;
20-
error 404 "Not in cache";
20+
error 204 "Purged (Not in cache)";
2121
}
22-
}
22+
}

tests/Unit/Invalidation/VarnishTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,38 @@ public function testSetServersThrowsInvalidServerException()
198198
new Varnish(array('http://127.0.0.1:80/some/weird/path'));
199199
}
200200

201+
public function testFlushCountSuccess()
202+
{
203+
$self = $this;
204+
$client = \Mockery::mock('\Guzzle\Http\Client[send]', array('', null))
205+
->shouldReceive('send')
206+
->once()
207+
->with(
208+
\Mockery::on(
209+
function ($requests) use ($self) {
210+
/** @type Request[] $requests */
211+
$self->assertCount(4, $requests);
212+
foreach ($requests as $request) {
213+
$self->assertEquals('PURGE', $request->getMethod());
214+
}
215+
216+
return true;
217+
}
218+
)
219+
)
220+
->getMock();
221+
222+
$varnish = new Varnish(array('127.0.0.1', '127.0.0.2'), 'fos.lo', $client);
223+
224+
$this->assertEquals(
225+
2,
226+
$varnish
227+
->purge('/c')
228+
->purge('/b')
229+
->flush()
230+
);
231+
}
232+
201233
protected function setUp()
202234
{
203235
$this->mock = new MockPlugin();

0 commit comments

Comments
 (0)