Skip to content

Commit e7295ef

Browse files
committed
add testing
1 parent d0f9078 commit e7295ef

11 files changed

+643
-250
lines changed

doc/symfony-cache-configuration.rst

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,101 @@ than using Varnish or NGINX, it can still provide considerable performance
99
gains over an installation that is not cached at all. It can be useful for
1010
running an application on shared hosting for instance.
1111

12-
You can use features of this library with the help of the
13-
``EventDispatchingHttpCache`` provided here. The basic concept is to use event
14-
subscribers on the HttpCache class.
12+
You can use features of this library with the help of event listeners that act
13+
on events of the ``HttpCache``. The Symfony ``HttpCache`` does not have an
14+
event system, for this you need to use the trait ``EventDispatchingHttpCache``
15+
provided by this library. The event listeners handle the requests from the
16+
:doc:`proxy-clients`.
1517

16-
.. warning::
18+
.. note::
1719

18-
If you are using the full stack Symfony framework, have a look at the
19-
HttpCache provided by the FOSHttpCacheBundle_ instead.
20+
Symfony ``HttpCache`` does not currently provide support for banning.
21+
22+
Using the trait
23+
~~~~~~~~~~~~~~~
2024

2125
.. note::
2226

23-
Symfony HttpCache does not currently provide support for banning.
27+
The trait is available since version 2.0.0. Version 1.* of this library
28+
instead provided a base ``HttpCache`` class to extend.
29+
30+
Your ``AppCache`` needs to implement ``CacheInvalidationInterface`` and use the
31+
trait ``FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache``::
32+
33+
use FOS\HttpCache\SymfonyCache\CacheInvalidationInterface;
34+
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
35+
use Symfony\Component\HttpFoundation\Request;
36+
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
37+
38+
class AppCache extends HttpCache implements CacheInvalidationInterface
39+
{
40+
use EventDispatchingHttpCache;
2441

25-
Extending the Correct HttpCache Class
26-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42+
/**
43+
* Made public to allow event subscribers to do refresh operations.
44+
*
45+
* {@inheritDoc}
46+
*/
47+
public function fetch(Request $request, $catch = false)
48+
{
49+
return parent::fetch($request, $catch);
50+
}
51+
}
2752

28-
Instead of extending ``Symfony\Component\HttpKernel\HttpCache\HttpCache``, your
29-
``AppCache`` should extend ``FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache``.
53+
The trait is adding events before and/or after kernel methods to let the
54+
listeners interfere. If you need to overwrite core ``HttpCache`` functionality
55+
in your kernel, one option is to provide your own event listeners. If you need
56+
to implement functionality directly on the methods, be careful to always call
57+
the trait methods rather than going directly to the parent, or events will not
58+
be triggered anymore. You might also need to copy a method from the trait and
59+
add your own logic between the events to not be too early or too late for the
60+
event.
3061

31-
.. tip::
62+
When starting to extend your ``AppCache``, it is recommended to use the
63+
``EventDispatchingHttpCacheTestCase`` to run tests with your kernel to be sure
64+
all events are triggered as expected.
3265

33-
If your class already needs to extend a different class, simply copy the
34-
event handling code from the EventDispatchingHttpCache into your
35-
``AppCache`` class and make it implement ``CacheInvalidationInterface``.
36-
The drawback is that you need to manually check whether you need to adjust
37-
your ``AppCache`` each time you update the FOSHttpCache library.
66+
Cache event listeners
67+
~~~~~~~~~~~~~~~~~~~~~
3868

3969
Now that you have an event dispatching kernel, you can make it register the
40-
subscribers you need. While you could do that from your bootstrap code, this is
70+
listeners you need. While you could do that from your bootstrap code, this is
4171
not the recommended way. You would need to adjust every place you instantiate
42-
the cache. Instead, overwrite the constructor of AppCache and register the
43-
subscribers there. A simple cache will look like this::
72+
the cache. Instead, overwrite the constructor of your ``AppCache`` and register
73+
the listeners you need there::
4474

45-
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
75+
use FOS\HttpCache\SymfonyCache\DebugListener();
76+
use FOS\HttpCache\SymfonyCache\CustomTtlListener();
4677
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
4778
use FOS\HttpCache\SymfonyCache\RefreshSubscriber;
4879
use FOS\HttpCache\SymfonyCache\UserContextSubscriber;
49-
use FOS\HttpCache\SymfonyCache\CustomTtlListener();
50-
51-
class AppCache extends EventDispatchingHttpCache
52-
{
53-
/**
54-
* Overwrite constructor to register event subscribers for FOSHttpCache.
55-
*/
56-
public function __construct(HttpKernelInterface $kernel, $cacheDir = null)
57-
{
58-
parent::__construct($kernel, $cacheDir);
5980

60-
$this->addSubscriber(new PurgeSubscriber());
61-
$this->addSubscriber(new RefreshSubscriber());
62-
$this->addSubscriber(new UserContextSubscriber());
63-
$this->addSubscriber(new CustomTtlListener());
81+
// ...
82+
83+
/**
84+
* Overwrite constructor to register event subscribers for FOSHttpCache.
85+
*/
86+
public function __construct(
87+
HttpKernelInterface $kernel,
88+
StoreInterface $store,
89+
SurrogateInterface $surrogate = null,
90+
array $options = array()
91+
) {
92+
parent::__construct($kernel, $store, $surrogate, $options);
93+
94+
$this->addSubscriber(new CustomTtlListener());
95+
$this->addSubscriber(new PurgeSubscriber());
96+
$this->addSubscriber(new RefreshSubscriber());
97+
$this->addSubscriber(new UserContextSubscriber());
98+
if (isset($options['debug']) && $options['debug']) {
99+
$this->addSubscriber(new DebugListener());
64100
}
65101
}
66102

103+
The event listeners can be tweaked by passing options to the constructor. The
104+
Symfony configuration system does not work here because things in the cache
105+
happen before the configuration is loaded.
106+
67107
Purge
68108
~~~~~
69109

@@ -202,28 +242,11 @@ Debugging
202242
~~~~~~~~~
203243

204244
For the ``assertHit`` and ``assertMiss`` assertions to work, you need to add
205-
debug information in your AppCache. Create the cache kernel with the option
206-
``'debug' => true`` and add the following to your ``AppCache``::
207-
208-
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
209-
{
210-
$response = parent::handle($request, $type, $catch);
211-
212-
if ($response->headers->has('X-Symfony-Cache')) {
213-
if (false !== strpos($response->headers->get('X-Symfony-Cache'), 'miss')) {
214-
$state = 'MISS';
215-
} elseif (false !== strpos($response->headers->get('X-Symfony-Cache'), 'fresh')) {
216-
$state = 'HIT';
217-
} else {
218-
$state = 'UNDETERMINED';
219-
}
220-
$response->headers->set('X-Cache', $state);
221-
}
222-
223-
return $response;
224-
}
245+
debug information in your AppCache. When running the tests, create the cache
246+
kernel with the option ``'debug' => true`` and add the ``DebugListener``.
225247

226-
The ``UNDETERMINED`` state should never happen. If it does, it means that your
227-
HttpCache is not correctly set into debug mode.
248+
The ``UNDETERMINED`` state should never happen. If it does, it means that
249+
something went really wrong in the kernel. Have a look at ``X-Symfony-Cache``
250+
and at the HTML body of the response.
228251

229252
.. _HttpCache: http://symfony.com/doc/current/book/http_cache.html#symfony-reverse-proxy

src/SymfonyCache/DebugSubscriber.php renamed to src/SymfonyCache/DebugListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* {@inheritdoc}
2929
*/
30-
class DebugSubscriber implements EventSubscriberInterface
30+
class DebugListener implements EventSubscriberInterface
3131
{
3232
/**
3333
* {@inheritdoc}

src/SymfonyCache/EventDispatchingHttpCache.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ protected function store(Request $request, Response $response)
9898
parent::store($request, $response);
9999
}
100100

101+
/**
102+
* {@inheritDoc}
103+
*
104+
* Adding the Events::PRE_INVALIDATE event.
105+
*/
106+
protected function invalidate(Request $request, $catch = false)
107+
{
108+
if ($response = $this->dispatch(Events::PRE_INVALIDATE, $request)) {
109+
return $response;
110+
}
111+
112+
return parent::invalidate($request, $catch);
113+
}
114+
101115
/**
102116
* Dispatch an event if needed.
103117
*
@@ -117,18 +131,4 @@ protected function dispatch($name, Request $request, Response $response = null)
117131

118132
return $response;
119133
}
120-
121-
/**
122-
* {@inheritDoc}
123-
*
124-
* Adding the Events::PRE_INVALIDATE event.
125-
*/
126-
protected function invalidate(Request $request, $catch = false)
127-
{
128-
if ($response = $this->dispatch(Events::PRE_INVALIDATE, $request)) {
129-
return $response;
130-
}
131-
132-
return parent::invalidate($request, $catch);
133-
}
134134
}

0 commit comments

Comments
 (0)