Skip to content

Commit 8e1ba5f

Browse files
committed
Merge pull request #38 from ddeboer/console-terminate
Flush cache manager at command termination (fix #32)
2 parents f90d412 + 188c0d2 commit 8e1ba5f

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

EventListener/InvalidationListener.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use FOS\HttpCacheBundle\Configuration\InvalidatePath;
77
use FOS\HttpCacheBundle\Configuration\InvalidateRoute;
88
use FOS\HttpCacheBundle\Invalidator\InvalidatorCollection;
9+
use Symfony\Component\Console\ConsoleEvents;
10+
use Symfony\Component\Console\Event\ConsoleEvent;
911
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1012
use Symfony\Component\ExpressionLanguage\SyntaxError;
1113
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
@@ -124,12 +126,37 @@ public function onKernelTerminate(PostResponseEvent $event)
124126
return $this->cacheManager->flush();
125127
}
126128

129+
/**
130+
* Flush cache manager when kernel exception occurs
131+
*/
132+
public function onKernelException()
133+
{
134+
$this->cacheManager->flush();
135+
}
136+
137+
/**
138+
* Flush cache manager when console terminates or errors
139+
*/
140+
public function onConsoleTerminate(ConsoleEvent $event)
141+
{
142+
$num = $this->cacheManager->flush();
143+
144+
if ($num > 0 && $event->getInput()->getOption('verbose')) {
145+
$event->getOutput()->writeln(sprintf('Sent %d invalidation requests', $num));
146+
}
147+
}
148+
127149
/**
128150
* {@inheritdoc}
129151
*/
130152
public static function getSubscribedEvents()
131153
{
132-
return array(KernelEvents::TERMINATE => 'onKernelTerminate');
154+
return array(
155+
KernelEvents::TERMINATE => 'onKernelTerminate',
156+
KernelEvents::EXCEPTION => 'onKernelException',
157+
ConsoleEvents::TERMINATE => 'onConsoleTerminate',
158+
ConsoleEvents::EXCEPTION => 'onConsoleTerminate'
159+
);
133160
}
134161

135162
/**

Resources/doc/cache-manager.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,12 @@ Flushing
130130
--------
131131

132132
Internally, the invalidation requests are queued and only sent out to your HTTP
133-
proxy when the manager is flushed. During HTTP requests, the manager is flushed
134-
automatically. If you want to invalidate objects outside request context, for
135-
instance from the command-line, you need to flush the cache manager manually
136-
(until https://github.com/ddeboer/FOSHttpCacheBundle/issues/32 is done):
133+
proxy when the manager is flushed. The manager is flushed automatically at the
134+
right moment:
137135

138-
```php
139-
$cacheManager
140-
->invalidateRoute(...)
141-
->invalidatePath(...)
142-
->flush();
143-
```
144-
145-
The performance impact of sending invalidation requests is kept to a minimum by:
136+
* when handling a HTTP request, after the response has been sent to the client
137+
(Symfony’s [kernel.terminate event](http://symfony.com/doc/current/components/http_kernel/introduction.html#the-kernel-terminate-event))
138+
* when running a console command, after the command has finished (Symfony’s
139+
[console.terminate event](http://symfony.com/doc/current/components/console/events.html#the-consoleevents-terminate-event)).
146140

147-
* flushing the cache manager only after the response by your controller has been sent to the client’s browser
148-
(during Symfony’s [kernel.terminate event](http://symfony.com/doc/current/components/http_kernel/introduction.html#the-kernel-terminate-event)).
149-
* sending all invalidation requests in parallel.
141+
You can also [flush the cache manager manually](https://github.com/FriendsOfSymfony/FOSHttpCache/blob/master/doc/cache-invalidator.md#flushing).

Tests/EventListener/InvalidationListenerTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,17 @@
22

33
namespace FOS\HttpCacheBundle\Tests\EventListener;
44

5-
use Doctrine\Common\Annotations\AnnotationReader;
6-
use FOS\HttpCache\Invalidation\Method\BanInterface;
7-
use FOS\HttpCache\Invalidation\Method\PurgeInterface;
8-
use FOS\HttpCacheBundle\Configuration\Invalidate;
95
use FOS\HttpCacheBundle\Configuration\InvalidatePath;
106
use FOS\HttpCacheBundle\Configuration\InvalidateRoute;
117
use FOS\HttpCacheBundle\EventListener\InvalidationListener;
128
use FOS\HttpCacheBundle\Invalidator\Invalidator;
139
use FOS\HttpCacheBundle\Invalidator\InvalidatorCollection;
14-
use FOS\HttpCacheBundle\Tests\EventListener\Fixture\FooControllerTagAtMethod;
15-
use Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener;
1610
use Symfony\Component\HttpFoundation\Request;
1711
use Symfony\Component\HttpFoundation\Response;
18-
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
1912
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
20-
use \Mockery;
21-
use Symfony\Component\HttpKernel\HttpKernelInterface;
2213
use Symfony\Component\Routing\Route;
2314
use Symfony\Component\Routing\RouteCollection;
15+
use \Mockery;
2416

2517
class InvalidationListenerTest extends \PHPUnit_Framework_TestCase
2618
{
@@ -133,6 +125,17 @@ public function testInvalidateRoute()
133125
$this->getListener()->onKernelTerminate($event);
134126
}
135127

128+
public function testOnConsoleTerminate()
129+
{
130+
$this->cacheManager->shouldReceive('flush')->once()->andReturn(2);
131+
132+
$event = \Mockery::mock('\Symfony\Component\Console\Event\ConsoleEvent');
133+
$event->shouldReceive('getInput->getOption')->with('verbose')->andReturn(true);
134+
$event->shouldReceive('getOutput->writeln')->with('Sent 2 invalidation requests')->once();
135+
136+
$this->getListener()->onConsoleTerminate($event);
137+
}
138+
136139
protected function getEvent(Request $request, Response $response = null)
137140
{
138141
return new PostResponseEvent(

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"mockery/mockery": "0.8.*",
3232
"monolog/monolog": "*",
3333
"sensio/framework-extra-bundle": "~2.3",
34+
"symfony/console": "~2.3",
3435
"symfony/security": "~2.3",
3536
"symfony/expression-language": "~2.4"
3637
},

0 commit comments

Comments
 (0)