@@ -27,8 +27,8 @@ Basic uses of the cache looks like this::
27
27
// ... and to remove the cache key
28
28
$pool->delete('my_cache_key');
29
29
30
- Symfony supports PSR-6 and PSR-16 cache interfaces. You can read more about
31
- these at the :doc: `component documentation </components/cache >`.
30
+ Symfony supports the Cache Contracts, PSR-6/16 and Doctrine Cache interfaces.
31
+ You can read more about these at the :doc: `component documentation </components/cache >`.
32
32
33
33
.. versionadded :: 4.2
34
34
@@ -92,24 +92,16 @@ adapter (template) they use by using the ``app`` and ``system`` key like:
92
92
],
93
93
]);
94
94
95
- The Cache component comes with a series of adapters already created :
95
+ The Cache component comes with a series of adapters pre-configured :
96
96
97
97
* :doc: `cache.adapter.apcu </components/cache/adapters/apcu_adapter >`
98
98
* :doc: `cache.adapter.array </components/cache/adapters/array_cache_adapter >`
99
99
* :doc: `cache.adapter.doctrine </components/cache/adapters/doctrine_adapter >`
100
100
* :doc: `cache.adapter.filesystem </components/cache/adapters/filesystem_adapter >`
101
101
* :doc: `cache.adapter.memcached </components/cache/adapters/memcached_adapter >`
102
102
* :doc: `cache.adapter.pdo </components/cache/adapters/pdo_doctrine_dbal_adapter >`
103
+ * :doc: `cache.adapter.psr6 </components/cache/adapters/proxy_adapter >`
103
104
* :doc: `cache.adapter.redis </components/cache/adapters/redis_adapter >`
104
- * :doc: `PHPFileAdapter </components/cache/adapters/php_files_adapter >`
105
- * :doc: `PHPArrayAdapter </components/cache/adapters/php_array_cache_adapter >`
106
-
107
- * :doc: `ChainAdapter </components/cache/adapters/chain_adapter >`
108
- * :doc: `ProxyAdapter </components/cache/adapters/proxy_adapter >`
109
- * ``cache.adapter.psr6 ``
110
-
111
- * ``cache.adapter.system ``
112
- * ``NullAdapter ``
113
105
114
106
Some of these adapters could be configured via shortcuts. Using these shortcuts
115
107
will create pool with service id of ``cache.[type] ``
@@ -183,10 +175,10 @@ will create pool with service id of ``cache.[type]``
183
175
],
184
176
]);
185
177
186
- Creating Custom Pools
187
- ---------------------
178
+ Creating Custom (Namespaced) Pools
179
+ ----------------------------------
188
180
189
- You can also create more customized pools. All you need is an adapter :
181
+ You can also create more customized pools:
190
182
191
183
.. configuration-block ::
192
184
@@ -196,15 +188,34 @@ You can also create more customized pools. All you need is an adapter:
196
188
framework :
197
189
cache :
198
190
default_memcached_provider : ' memcached://localhost'
191
+
199
192
pools :
193
+ # creates a "custom_thing.cache" service
194
+ # autowireable via "CacheInterface $customThingCache"
195
+ # uses the "app" cache configuration
196
+ custom_thing.cache :
197
+ adapter : cache.app
198
+
199
+ # creates a "my_cache_pool" service
200
+ # autowireable via "CacheInterface $myCachePool"
200
201
my_cache_pool :
201
202
adapter : cache.adapter.array
202
- cache.acme :
203
+
204
+ # uses the default_memcached_provider from above
205
+ acme.cache :
203
206
adapter : cache.adapter.memcached
204
- cache.foobar :
207
+
208
+ # control adapter's configuration
209
+ foobar.cache :
205
210
adapter : cache.adapter.memcached
206
211
provider : ' memcached://user:[email protected] '
207
212
213
+ # uses the "foobar.cache" pool as its backend but controls
214
+ # the lifetime and (like all pools) has a separate cache namespace
215
+ short_cache :
216
+ adapter : cache.foobar
217
+ default_lifetime : 60
218
+
208
219
.. code-block :: xml
209
220
210
221
<!-- config/packages/cache.xml -->
@@ -217,9 +228,11 @@ You can also create more customized pools. All you need is an adapter:
217
228
218
229
<framework : config >
219
230
<framework : cache default_memcached_provider =" memcached://localhost" >
231
+ <framework : pool name =" custom_thing.cache" adapter =" cache.app" />
220
232
<framework : pool name =" my_cache_pool" adapter =" cache.adapter.array" />
221
- <framework : pool name =" cache.acme" adapter =" cache.adapter.memcached" />
222
- <framework : pool name =" cache.foobar" adapter =" cache.adapter.memcached" provider =" memcached://user:[email protected] " />
233
+ <framework : pool name =" acme.cache" adapter =" cache.adapter.memcached" />
234
+ <framework : pool name =" foobar.cache" adapter =" cache.adapter.memcached" provider =" memcached://user:[email protected] " />
235
+ <framework : pool name =" short_cache" adapter =" foobar.cache" default_lifetime =" 60" />
223
236
</framework : cache >
224
237
</framework : config >
225
238
</container >
@@ -231,101 +244,54 @@ You can also create more customized pools. All you need is an adapter:
231
244
'cache' => [
232
245
'default_memcached_provider' => 'memcached://localhost',
233
246
'pools' => [
247
+ 'custom_thing.cache' => [
248
+ 'adapter' => 'cache.app',
249
+ ],
234
250
'my_cache_pool' => [
235
251
'adapter' => 'cache.adapter.array',
236
252
],
237
- 'cache. acme' => [
253
+ 'acme.cache ' => [
238
254
'adapter' => 'cache.adapter.memcached',
239
255
],
240
- 'cache.foobar' => [
241
- 'adapter' => 'cache.adapter.memcached',
242
- 'provider' => 'memcached://user:[email protected] ',
243
- ],
244
- ],
245
- ],
246
- ]);
247
-
248
-
249
- The configuration above will create 3 services: ``my_cache_pool ``, ``cache.acme ``
250
- and ``cache.foobar ``. The ``my_cache_pool `` pool is using the ArrayAdapter
251
- and the other two are using the :doc: `MemcachedAdapter </components/cache/adapters/memcached_adapter >`.
252
- The ``cache.acme `` pool is using the Memcached server on localhost and ``cache.foobar ``
253
- is using the Memcached server at example.com.
254
-
255
- For advanced configurations it could sometimes be useful to use a pool as an adapter.
256
-
257
- .. configuration-block ::
258
-
259
- .. code-block :: yaml
260
-
261
- # config/packages/cache.yaml
262
- framework :
263
- cache :
264
- app : my_configured_app_cache
265
- pools :
266
- my_cache_pool :
267
- adapter : cache.adapter.memcached
268
- provider : ' memcached://user:[email protected] '
269
- cache.short_cache :
270
- adapter : my_cache_pool
271
- default_lifetime : 60
272
- cache.long_cache :
273
- adapter : my_cache_pool
274
- default_lifetime : 604800
275
- my_configured_app_cache :
276
- # "cache.adapter.filesystem" is the default for "cache.app"
277
- adapter : cache.adapter.filesystem
278
- default_lifetime : 3600
279
-
280
- .. code-block :: xml
281
-
282
- <!-- config/packages/cache.xml -->
283
- <?xml version =" 1.0" encoding =" UTF-8" ?>
284
- <container xmlns =" http://symfony.com/schema/dic/services"
285
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
286
- xmlns : framework =" http://symfony.com/schema/dic/symfony"
287
- xsi : schemaLocation =" http://symfony.com/schema/dic/services
288
- https://symfony.com/schema/dic/services/services-1.0.xsd" >
289
-
290
- <framework : config >
291
- <framework : cache app =" my_cache_pool" >
292
- <framework : pool name =" my_cache_pool" adapter =" cache.adapter.memcached" provider =" memcached://user:[email protected] " />
293
- <framework : pool name =" cache.short_cache" adapter =" my_cache_pool" default_lifetime =" 604800" />
294
- <framework : pool name =" cache.long_cache" adapter =" my_cache_pool" default_lifetime =" 604800" />
295
- <!-- "cache.adapter.filesystem" is the default for "cache.app" -->
296
- <framework : pool name =" my_configured_app_cache" adapter =" cache.adapter.filesystem" default_lifetime =" 3600" />
297
- </framework : cache >
298
- </framework : config >
299
- </container >
300
-
301
- .. code-block :: php
302
-
303
- // config/packages/cache.php
304
- $container->loadFromExtension('framework', [
305
- 'cache' => [
306
- 'app' => 'my_configured_app_cache',
307
- 'pools' => [
308
- 'my_cache_pool' => [
256
+ 'foobar.cache' => [
309
257
'adapter' => 'cache.adapter.memcached',
310
258
'provider' => 'memcached://user:[email protected] ',
311
259
],
312
- 'cache. short_cache' => [
313
- 'adapter' => 'cache.adapter.memcached ',
260
+ 'short_cache' => [
261
+ 'adapter' => 'foobar.cache ',
314
262
'default_lifetime' => 60,
315
263
],
316
- 'cache.long_cache' => [
317
- 'adapter' => 'cache.adapter.memcached',
318
- 'default_lifetime' => 604800,
319
- ],
320
- 'my_configured_app_cache' => [
321
- // "cache.adapter.filesystem" is the default for "cache.app"
322
- 'adapter' => 'cache.adapter.filesystem',
323
- 'default_lifetime' => 3600,
324
- ],
325
264
],
326
265
],
327
266
]);
328
267
268
+ Each pool manages a set of independent cache keys: keys of different pools
269
+ *never * collide, even if they share the same backend. This is achieved by prefixing
270
+ keys with a namespace that's generated by hashing the name of the pool, the name
271
+ of the compiled container class and a :ref: `configurable seed<reference-cache-prefix-seed> `
272
+ that defaults to the project directory.
273
+
274
+ Each custom pool becomes a service where the service id is the name of the pool
275
+ (e.g. ``custom_thing.cache ``). An autowiring alias is also created for each pool
276
+ using the camel case version of its name - e.g. ``custom_thing.cache `` can be
277
+ injected automatically by naming the argument ``$customThingCache `` and type-hinting it
278
+ with either :class: `Symfony\\ Contracts\\ Cache\\ CacheInterface ` or
279
+ ``Psr\\Cache\\CacheItemPoolInterface ``::
280
+
281
+ use Symfony\Contracts\Cache\CacheInterface;
282
+
283
+ // from a controller method
284
+ public function listProducts(CacheInterface $customThingCache)
285
+ {
286
+ // ...
287
+ }
288
+
289
+ // in a service
290
+ public function __construct(CacheInterface $customThingCache)
291
+ {
292
+ // ...
293
+ }
294
+
329
295
Custom Provider Options
330
296
-----------------------
331
297
0 commit comments