Skip to content

Commit 7ab71ba

Browse files
committed
feature #11477 Update Cache component doc for 4.2 (nicolas-grekas, weaverryan)
This PR was merged into the 4.2 branch. Discussion ---------- Update Cache component doc for 4.2 From the plane to SymfonyLive Tunis :) Fixes #9851 Commits ------- 3209040 typo 4f7b9c4 Merge branch '4.2' into cache-contracts 50736c7 clarifying d413993 tweaks b2bb888 adding anchor 67243ff proofing cache chapter updates 808453a Update Cache component doc for 4.2
2 parents de23479 + 3209040 commit 7ab71ba

13 files changed

+215
-194
lines changed

cache.rst

Lines changed: 66 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Basic uses of the cache looks like this::
2727
// ... and to remove the cache key
2828
$pool->delete('my_cache_key');
2929

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>`.
3232

3333
.. versionadded:: 4.2
3434

@@ -92,24 +92,16 @@ adapter (template) they use by using the ``app`` and ``system`` key like:
9292
],
9393
]);
9494
95-
The Cache component comes with a series of adapters already created:
95+
The Cache component comes with a series of adapters pre-configured:
9696

9797
* :doc:`cache.adapter.apcu </components/cache/adapters/apcu_adapter>`
9898
* :doc:`cache.adapter.array </components/cache/adapters/array_cache_adapter>`
9999
* :doc:`cache.adapter.doctrine </components/cache/adapters/doctrine_adapter>`
100100
* :doc:`cache.adapter.filesystem </components/cache/adapters/filesystem_adapter>`
101101
* :doc:`cache.adapter.memcached </components/cache/adapters/memcached_adapter>`
102102
* :doc:`cache.adapter.pdo </components/cache/adapters/pdo_doctrine_dbal_adapter>`
103+
* :doc:`cache.adapter.psr6 </components/cache/adapters/proxy_adapter>`
103104
* :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``
113105

114106
Some of these adapters could be configured via shortcuts. Using these shortcuts
115107
will create pool with service id of ``cache.[type]``
@@ -183,10 +175,10 @@ will create pool with service id of ``cache.[type]``
183175
],
184176
]);
185177
186-
Creating Custom Pools
187-
---------------------
178+
Creating Custom (Namespaced) Pools
179+
----------------------------------
188180

189-
You can also create more customized pools. All you need is an adapter:
181+
You can also create more customized pools:
190182

191183
.. configuration-block::
192184

@@ -196,15 +188,34 @@ You can also create more customized pools. All you need is an adapter:
196188
framework:
197189
cache:
198190
default_memcached_provider: 'memcached://localhost'
191+
199192
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"
200201
my_cache_pool:
201202
adapter: cache.adapter.array
202-
cache.acme:
203+
204+
# uses the default_memcached_provider from above
205+
acme.cache:
203206
adapter: cache.adapter.memcached
204-
cache.foobar:
207+
208+
# control adapter's configuration
209+
foobar.cache:
205210
adapter: cache.adapter.memcached
206211
provider: 'memcached://user:[email protected]'
207212
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+
208219
.. code-block:: xml
209220
210221
<!-- config/packages/cache.xml -->
@@ -217,9 +228,11 @@ You can also create more customized pools. All you need is an adapter:
217228
218229
<framework:config>
219230
<framework:cache default_memcached_provider="memcached://localhost">
231+
<framework:pool name="custom_thing.cache" adapter="cache.app"/>
220232
<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"/>
223236
</framework:cache>
224237
</framework:config>
225238
</container>
@@ -231,101 +244,54 @@ You can also create more customized pools. All you need is an adapter:
231244
'cache' => [
232245
'default_memcached_provider' => 'memcached://localhost',
233246
'pools' => [
247+
'custom_thing.cache' => [
248+
'adapter' => 'cache.app',
249+
],
234250
'my_cache_pool' => [
235251
'adapter' => 'cache.adapter.array',
236252
],
237-
'cache.acme' => [
253+
'acme.cache' => [
238254
'adapter' => 'cache.adapter.memcached',
239255
],
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' => [
309257
'adapter' => 'cache.adapter.memcached',
310258
'provider' => 'memcached://user:[email protected]',
311259
],
312-
'cache.short_cache' => [
313-
'adapter' => 'cache.adapter.memcached',
260+
'short_cache' => [
261+
'adapter' => 'foobar.cache',
314262
'default_lifetime' => 60,
315263
],
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-
],
325264
],
326265
],
327266
]);
328267
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+
329295
Custom Provider Options
330296
-----------------------
331297

0 commit comments

Comments
 (0)