Skip to content

Commit cf8524e

Browse files
committed
Flush cache manager at command termination (fix #32)
Flush cache manager on kernel and command exception, too Revert minimum-stability to dev Reduce number of terminate methods
1 parent f90d412 commit cf8524e

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

EventListener/InvalidationListener.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
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;
1214
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1316
use Symfony\Component\HttpKernel\KernelEvents;
1417
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
1518
use Symfony\Component\Routing\RouterInterface;
@@ -124,12 +127,25 @@ public function onKernelTerminate(PostResponseEvent $event)
124127
return $this->cacheManager->flush();
125128
}
126129

130+
/**
131+
* Flush cache manager on console termination and exception
132+
*/
133+
public function onTerminate()
134+
{
135+
$this->cacheManager->flush();
136+
}
137+
127138
/**
128139
* {@inheritdoc}
129140
*/
130141
public static function getSubscribedEvents()
131142
{
132-
return array(KernelEvents::TERMINATE => 'onKernelTerminate');
143+
return array(
144+
KernelEvents::TERMINATE => 'onKernelTerminate',
145+
KernelEvents::EXCEPTION => 'onTerminate',
146+
ConsoleEvents::TERMINATE => 'onTerminate',
147+
ConsoleEvents::EXCEPTION => 'onTerminate'
148+
);
133149
}
134150

135151
/**

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: 8 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,13 @@ public function testInvalidateRoute()
133125
$this->getListener()->onKernelTerminate($event);
134126
}
135127

128+
public function testOnTerminate()
129+
{
130+
$this->cacheManager->shouldReceive('flush')->once();
131+
132+
$this->getListener()->onTerminate();
133+
}
134+
136135
protected function getEvent(Request $request, Response $response = null)
137136
{
138137
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)