@@ -9,61 +9,101 @@ than using Varnish or NGINX, it can still provide considerable performance
9
9
gains over an installation that is not cached at all. It can be useful for
10
10
running an application on shared hosting for instance.
11
11
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 `.
15
17
16
- .. warning ::
18
+ .. note ::
17
19
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
+ ~~~~~~~~~~~~~~~
20
24
21
25
.. note ::
22
26
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;
24
41
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
+ }
27
52
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.
30
61
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.
32
65
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
+ ~~~~~~~~~~~~~~~~~~~~~
38
68
39
69
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
41
71
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 ::
44
74
45
- use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
75
+ use FOS\HttpCache\SymfonyCache\DebugListener();
76
+ use FOS\HttpCache\SymfonyCache\CustomTtlListener();
46
77
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
47
78
use FOS\HttpCache\SymfonyCache\RefreshSubscriber;
48
79
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);
59
80
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());
64
100
}
65
101
}
66
102
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
+
67
107
Purge
68
108
~~~~~
69
109
@@ -204,28 +244,11 @@ Debugging
204
244
~~~~~~~~~
205
245
206
246
For the ``assertHit `` and ``assertMiss `` assertions to work, you need to add
207
- debug information in your AppCache. Create the cache kernel with the option
208
- ``'debug' => true `` and add the following to your ``AppCache ``::
209
-
210
- public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
211
- {
212
- $response = parent::handle($request, $type, $catch);
213
-
214
- if ($response->headers->has('X-Symfony-Cache')) {
215
- if (false !== strpos($response->headers->get('X-Symfony-Cache'), 'miss')) {
216
- $state = 'MISS';
217
- } elseif (false !== strpos($response->headers->get('X-Symfony-Cache'), 'fresh')) {
218
- $state = 'HIT';
219
- } else {
220
- $state = 'UNDETERMINED';
221
- }
222
- $response->headers->set('X-Cache', $state);
223
- }
224
-
225
- return $response;
226
- }
247
+ debug information in your AppCache. When running the tests, create the cache
248
+ kernel with the option ``'debug' => true `` and add the ``DebugListener ``.
227
249
228
- The ``UNDETERMINED `` state should never happen. If it does, it means that your
229
- HttpCache is not correctly set into debug mode.
250
+ The ``UNDETERMINED `` state should never happen. If it does, it means that
251
+ something went really wrong in the kernel. Have a look at ``X-Symfony-Cache ``
252
+ and at the HTML body of the response.
230
253
231
254
.. _HttpCache : http://symfony.com/doc/current/book/http_cache.html#symfony-reverse-proxy
0 commit comments