1
1
Cache
2
2
=====
3
3
4
- .. include :: ../_outdate-caution.rst.inc
4
+ The Symfony2 CMF BlockBundle integrates with the `SonataCacheBundle `_ to
5
+ provide several caching solutions. Have a look at the available adapters in the
6
+ `SonataCacheBundle `_ to see all options.
5
7
6
- The BlockBundle integrates with the `SonataCacheBundle `_ to provide several
7
- caching solutions. Have a look at the available adapters in the
8
- SonataCacheBundle to see all options.
9
-
10
- The BlockBundle additionally provides its own adapters for:
8
+ The Symfony2 CMF BlockBundle additionally provides its own adapters for:
11
9
12
10
* `ESI `_
13
11
* `SSI `_
@@ -58,7 +56,8 @@ CmfBlockBundle and the SonataBlockBundle:
58
56
<!-- ... -->
59
57
60
58
<import resource =" @SymfonyCmfBlockBundle/Resources/config/routing/cache.xml"
61
- prefix =" /" />
59
+ prefix =" /"
60
+ />
62
61
</routes >
63
62
64
63
.. code-block :: php
@@ -74,70 +73,82 @@ CmfBlockBundle and the SonataBlockBundle:
74
73
3. *SonataBlockBundle * - Use the ``sonata_block `` key to configure the cache
75
74
adapter for each block service:
76
75
77
- .. configuration-block ::
76
+ .. configuration-block ::
78
77
79
- .. code-block :: yaml
78
+ .. code-block :: yaml
80
79
81
- # app/config/config.yml
82
- sonata_block :
83
- # ...
84
- blocks :
85
- acme_main.block.news :
86
- # use the service id of the cache adapter
87
- cache : cmf.block.cache.js_async
88
- blocks_by_class :
89
- # cache only the RssBlock and not all action blocks
90
- Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\RssBlock :
80
+ # app/config/config.yml
81
+ sonata_block :
82
+ # ...
83
+ blocks :
84
+ symfony_cmf.block.action :
85
+ # use the service id of the cache adapter
91
86
cache : cmf.block.cache.js_async
87
+ blocks_by_class :
88
+ # cache only the RssBlock and not all action blocks
89
+ Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\RssBlock :
90
+ cache : cmf.block.cache.js_async
92
91
93
- .. code-block :: xml
92
+ .. code-block :: xml
94
93
95
- <!-- app/config/config.xml -->
96
- <?xml version =" 1.0" charset =" UTF-8" ?>
97
- <container xmlns =" http://symfony.com/schema/dic/services" >
94
+ <!-- app/config/config.xml -->
95
+ <?xml version =" 1.0" charset =" UTF-8" ?>
96
+ <container xmlns =" http://symfony.com/schema/dic/services" >
98
97
99
98
<config xmlns =" http://sonata-project.org/schema/dic/block" >
100
99
<!-- use the service id of the cache adapter -->
101
- <blocks id =" symfony_cmf.block.action"
100
+ <block
101
+ id =" symfony_cmf.block.action"
102
+ cache =" symfony_cmf.block.cache.js_async"
103
+ />
104
+ <block-by-class
105
+ class =" Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\RssBlock"
102
106
cache =" symfony_cmf.block.cache.js_async"
103
107
/>
104
108
</config >
105
109
</container >
106
110
107
- .. code-block :: php
108
-
109
- // app/config/config.php
110
- $container->loadFromExtension('sonata_block', array(
111
- 'blocks' => array(
112
- 'symfony_cmf.block.action' => array(
113
- // use the service id of the cache adapter
114
- 'cache' => 'symfony_cmf.block.cache.js_async',
115
- ),
116
- ),
117
- ));
111
+ .. code-block :: php
112
+
113
+ // app/config/config.php
114
+ $container->loadFromExtension('sonata_block', array(
115
+ 'blocks' => array(
116
+ 'symfony_cmf.block.action' => array(
117
+ // use the service id of the cache adapter
118
+ 'cache' => 'symfony_cmf.block.cache.js_async',
119
+ ),
120
+ ),
121
+ 'blocks_by_class' => array(
122
+ 'Symfony\Cmf\Bundle\BlockBundle\Doctrine\Phpcr\RssBlock' => array(
123
+ 'cache' => 'symfony_cmf.block.cache.js_async',
124
+ ),
125
+ ),
126
+ ),
127
+ ));
118
128
119
129
Workflow
120
130
--------
121
131
122
- The following happens when a block is rendered using cache:
132
+ When a block having a cache configured is rendered, the following process
133
+ is triggered:
123
134
124
- * A document is loaded based on the name
135
+ * The document is loaded based on the name;
125
136
* If caching is configured, the cache is checked and content is returned if
126
- found
137
+ found.
127
138
128
139
* Cache keys are computed using:
129
140
130
- * The cache keys of the block service
131
- * The extraCacheKeys passed from the template
141
+ * The cache keys of the block service;
142
+ * The extraCacheKeys passed from the template.
132
143
133
- * The cache adapter is asked for a cache element
144
+ * The cache adapter is asked for a cache element:
134
145
135
146
* The ESI and SSI adapter add a specific tag and a url to retrieve the
136
- block content
137
- * The Javascript adapter adds javascript and a url to retrieve the block
138
- content
147
+ block content;
148
+ * The javascript adapter adds javascript and a url to retrieve the block
149
+ content.
139
150
140
- * If the cache element is not expired and has data it is returned
151
+ * If the cache element is not expired and has data it is returned.
141
152
* The template is rendered:
142
153
143
154
* For ESI and SSI the url is called to retrieve the block content
@@ -150,55 +161,60 @@ The following happens when a block is rendered using cache:
150
161
cache is found, have a look at the ``has `` method of the adapters in the
151
162
SonataCacheBundle to see how they respond.
152
163
153
- If cache is checked and the cache adapter returned that no cache was found,
164
+ If the cache is checked and the cache adapter returned that no cache was found,
154
165
the workflow proceeds like this:
155
166
156
- * Each block document also has a block service, the execute method of it is
157
- called to render the block and return a response
158
- * If the response is cacheable the configured adapter creates a cache element,
159
- it contains
167
+ * The block service is asked to render the block
168
+ :ref: ` as usual < bundle- block-execute >`;
169
+ * If the `` Response `` is cacheable, the configured adapter creates a cache
170
+ element containing:
160
171
161
- * The computed cache keys
162
- * The ttl of the response
163
- * The response
164
- * And additional contextual keys
172
+ * The computed cache keys;
173
+ * The time to live (TTL) of the response;
174
+ * The `` Response ``;
175
+ * Any additional contextual keys.
165
176
166
- * The template is rendered
177
+ * The template is rendered.
167
178
168
179
Cache Keys
169
180
----------
170
181
171
- The block service has the responsibility to generate the cache keys, the
172
- method ``getCacheKeys `` returns these keys, see :ref: ` bundle-block-service `.
182
+ It is the responsibility of the :ref: ` block service < bundle-block-service >` to generate the cache keys in
183
+ the method ``getCacheKeys ``.
173
184
174
- The block services shipped with the BlockBunde use the ``getCacheKeys `` method
175
- of the ``Sonata\BlockBundle\Block\BaseBlockService ``, and return:
185
+ The block services shipped with the Symfony CMF BlockBunde use the
186
+ ``getCacheKeys `` method of the ``Sonata\BlockBundle\Block\BaseBlockService ``
187
+ which returns:
176
188
177
189
* ``block_id ``
178
190
* ``updated_at ``
179
191
180
- .. note ::
192
+ .. caution ::
181
193
182
- If block settings need to be persisted between requests it is advised to
183
- store them in the block document. Alternatively they can be added to the
184
- cache keys. However be very cautious because, depending on the adapter,
185
- the cache keys can be send to the browser and are not secure.
194
+ If block settings need to be persisted between requests, it is advised to
195
+ store them in the block document. If you add them to the cache keys, you
196
+ have to be aware that depending on the adapter, the cache keys can be sent
197
+ to the browser and thus are neither hidden nor safe from manipulation by a
198
+ client.
186
199
187
200
Extra Cache Keys
188
201
~~~~~~~~~~~~~~~~
189
202
190
203
The extra cache keys array is used to store metadata along the cache element.
191
- The metadata can be used to invalidate a set of cache elements.
204
+ The metadata can be used to invalidate a set of cached elements.
192
205
193
206
Contextual Keys
194
207
~~~~~~~~~~~~~~~
195
208
196
209
The contextual cache array hold the object class and id used inside the
197
210
template. This contextual cache array is then added to the extra cache key.
198
211
199
- This feature can be use like this ``$cacheManager->remove(array('objectId' => 'id')) ``.
212
+ This feature can be use like this::
213
+
214
+ $cacheManager->remove(array('objectId' => 'id'));
200
215
201
- Of course not all cache adapters support this feature, varnish and MongoDB do.
216
+ While not all cache adapters support this feature, the Varnish and MongoDB
217
+ adapters do.
202
218
203
219
The BlockBundle also has a cache invalidation listener that calls the
204
220
``flush `` method of a cache adapter automatically when a cached block document
@@ -235,7 +251,7 @@ your Twig template when using cache:
235
251
),
236
252
)) ?>
237
253
238
- When using the Esi, Ssi or Js cache adapters the settings passed here are remembered:
254
+ When using the Esi, Ssi or Js cache adapters, the settings passed here are remembered:
239
255
240
256
.. configuration-block ::
241
257
@@ -269,9 +285,9 @@ also the `SonataBlockBundle Advanced usage`_ documentation.
269
285
270
286
.. caution ::
271
287
272
- Because, as mentioned above, settings can be added to the url as parameters
273
- avoid exposing sensitive settings from `` sonata_block_render `` and try to
274
- store them in the block document.
288
+ Because, as mentioned above, settings can be added to the URL as
289
+ parameters, you have to avoid exposing sensitive settings from
290
+ `` sonata_block_render `` and store them in the block document instead .
275
291
276
292
Adapters
277
293
--------
0 commit comments