@@ -175,10 +175,10 @@ will create pool with service id of ``cache.[type]``
175175 ],
176176 ]);
177177
178- Creating Custom Pools
179- ---------------------
178+ Creating Custom (Namespaced) Pools
179+ ----------------------------------
180180
181- You can also create more customized pools. All you need is an adapter :
181+ You can also create more customized pools:
182182
183183.. configuration-block ::
184184
@@ -188,15 +188,34 @@ You can also create more customized pools. All you need is an adapter:
188188 framework :
189189 cache :
190190 default_memcached_provider : ' memcached://localhost'
191+
191192 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"
192201 my_cache_pool :
193202 adapter : cache.adapter.array
194- cache.acme :
203+
204+ # uses the default_memcached_provider from above
205+ acme.cache :
195206 adapter : cache.adapter.memcached
196- cache.foobar :
207+
208+ # control adapter's configuration
209+ foobar.cache :
197210 adapter : cache.adapter.memcached
198211 provider : ' memcached://user:password@example.com'
199212
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+
200219 .. code-block :: xml
201220
202221 <!-- config/packages/cache.xml -->
@@ -209,9 +228,11 @@ You can also create more customized pools. All you need is an adapter:
209228
210229 <framework : config >
211230 <framework : cache default_memcached_provider =" memcached://localhost" >
231+ <framework : pool name =" custom_thing.cache" adapter =" cache.app" />
212232 <framework : pool name =" my_cache_pool" adapter =" cache.adapter.array" />
213- <framework : pool name =" cache.acme" adapter =" cache.adapter.memcached" />
214- <framework : pool name =" cache.foobar" adapter =" cache.adapter.memcached" provider =" memcached://user:password@example.com" />
233+ <framework : pool name =" acme.cache" adapter =" cache.adapter.memcached" />
234+ <framework : pool name =" foobar.cache" adapter =" cache.adapter.memcached" provider =" memcached://user:password@example.com" />
235+ <framework : pool name =" short_cache" adapter =" foobar.cache" default_lifetime =" 60" />
215236 </framework : cache >
216237 </framework : config >
217238 </container >
@@ -223,107 +244,54 @@ You can also create more customized pools. All you need is an adapter:
223244 'cache' => [
224245 'default_memcached_provider' => 'memcached://localhost',
225246 'pools' => [
247+ 'custom_thing.cache' => [
248+ 'adapter' => 'cache.app',
249+ ],
226250 'my_cache_pool' => [
227251 'adapter' => 'cache.adapter.array',
228252 ],
229- 'cache. acme' => [
253+ 'acme.cache ' => [
230254 'adapter' => 'cache.adapter.memcached',
231255 ],
232- 'cache.foobar' => [
233- 'adapter' => 'cache.adapter.memcached',
234- 'provider' => 'memcached://user:password@example.com',
235- ],
236- ],
237- ],
238- ]);
239-
240-
241- The configuration above will create 3 services: ``my_cache_pool ``, ``cache.acme ``
242- and ``cache.foobar ``. The ``my_cache_pool `` pool is using the ArrayAdapter
243- and the other two are using the :doc: `MemcachedAdapter </components/cache/adapters/memcached_adapter >`.
244- The ``cache.acme `` pool is using the Memcached server on localhost and ``cache.foobar ``
245- is using the Memcached server at example.com.
246-
247- Each pool will manage a set of independent cache keys: keys of different pools
248- never collide even if they share the same backend. This is achieved by prefixing
249- keys with a namespace. This namespace is generated by hashing the name of the
250- pool, the name of the compiled container class and a configurable seed that XXX HOWTO LINK TO reference/configuration/framework.rst#prefix_seed? XXX
251- defaults to the project directory.
252-
253- For advanced configurations it could sometimes be useful to use a pool as an adapter.
254-
255- .. configuration-block ::
256-
257- .. code-block :: yaml
258-
259- # config/packages/cache.yaml
260- framework :
261- cache :
262- app : my_configured_app_cache
263- pools :
264- my_cache_pool :
265- adapter : cache.adapter.memcached
266- provider : ' memcached://user:password@example.com'
267- cache.short_cache :
268- adapter : my_cache_pool
269- default_lifetime : 60
270- cache.long_cache :
271- adapter : my_cache_pool
272- default_lifetime : 604800
273- my_configured_app_cache :
274- # "cache.adapter.filesystem" is the default for "cache.app"
275- adapter : cache.adapter.filesystem
276- default_lifetime : 3600
277-
278- .. code-block :: xml
279-
280- <!-- config/packages/cache.xml -->
281- <?xml version =" 1.0" encoding =" UTF-8" ?>
282- <container xmlns =" http://symfony.com/schema/dic/services"
283- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
284- xmlns : framework =" http://symfony.com/schema/dic/symfony"
285- xsi : schemaLocation =" http://symfony.com/schema/dic/services
286- https://symfony.com/schema/dic/services/services-1.0.xsd" >
287-
288- <framework : config >
289- <framework : cache app =" my_cache_pool" >
290- <framework : pool name =" my_cache_pool" adapter =" cache.adapter.memcached" provider =" memcached://user:password@example.com" />
291- <framework : pool name =" cache.short_cache" adapter =" my_cache_pool" default_lifetime =" 604800" />
292- <framework : pool name =" cache.long_cache" adapter =" my_cache_pool" default_lifetime =" 604800" />
293- <!-- "cache.adapter.filesystem" is the default for "cache.app" -->
294- <framework : pool name =" my_configured_app_cache" adapter =" cache.adapter.filesystem" default_lifetime =" 3600" />
295- </framework : cache >
296- </framework : config >
297- </container >
298-
299- .. code-block :: php
300-
301- // config/packages/cache.php
302- $container->loadFromExtension('framework', [
303- 'cache' => [
304- 'app' => 'my_configured_app_cache',
305- 'pools' => [
306- 'my_cache_pool' => [
256+ 'foobar.cache' => [
307257 'adapter' => 'cache.adapter.memcached',
308258 'provider' => 'memcached://user:password@example.com',
309259 ],
310- 'cache. short_cache' => [
311- 'adapter' => 'cache.adapter.memcached ',
260+ 'short_cache' => [
261+ 'adapter' => 'foobar.cache ',
312262 'default_lifetime' => 60,
313263 ],
314- 'cache.long_cache' => [
315- 'adapter' => 'cache.adapter.memcached',
316- 'default_lifetime' => 604800,
317- ],
318- 'my_configured_app_cache' => [
319- // "cache.adapter.filesystem" is the default for "cache.app"
320- 'adapter' => 'cache.adapter.filesystem',
321- 'default_lifetime' => 3600,
322- ],
323264 ],
324265 ],
325266 ]);
326267
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 ``$forecastCache `` 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+
327295Custom Provider Options
328296-----------------------
329297
@@ -401,19 +369,6 @@ and use that when configuring the pool.
401369 'timeout' => 10
402370 ]);
403371
404- Injecting a custom pool into your services
405- ------------------------------------------
406-
407- Each custom pool is turned into a corresponding service with the exact same id as the
408- name of the pool that you can reference in your service configuration files.
409-
410- If you prefer using autowiring, a named autowiring alias is created for each pool
411- using the camel case version of its name. This means that if you name a pool e.g.
412- ``forecast.cache ``, you can have it injected automatically by naming the
413- corresponding argument ``$forecastCache `` with its type-hint set to either
414- ``Symfony\\Contracts\\Cache\\CacheInterface `` or ``Psr\\Cache\\CacheItemPoolInterface ``
415- depending on the interfaces you want to use.
416-
417372 Creating a Cache Chain
418373----------------------
419374
0 commit comments