Skip to content

Commit eb27aca

Browse files
authored
Update documentation regarding caching (#9043)
1 parent f414e57 commit eb27aca

File tree

3 files changed

+72
-343
lines changed

3 files changed

+72
-343
lines changed

docs/en/reference/advanced-configuration.rst

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@ steps of configuration.
99
.. code-block:: php
1010
1111
<?php
12-
use Doctrine\ORM\EntityManager,
13-
Doctrine\ORM\Configuration;
12+
13+
use Doctrine\ORM\Configuration;
14+
use Doctrine\ORM\EntityManager;
15+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
16+
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
1417
1518
// ...
1619
1720
if ($applicationMode == "development") {
18-
$cache = new \Doctrine\Common\Cache\ArrayCache;
21+
$queryCache = new ArrayAdapter();
22+
$metadataCache = new ArrayAdapter();
1923
} else {
20-
$cache = new \Doctrine\Common\Cache\ApcCache;
24+
$queryCache = new PhpFilesAdapter('doctrine_queries');
25+
$metadataCache = new PhpFilesAdapter('doctrine_metadata');
2126
}
2227
2328
$config = new Configuration;
24-
$config->setMetadataCacheImpl($cache);
29+
$config->setMetadataCache($metadataCache);
2530
$driverImpl = $config->newDefaultAnnotationDriver('/path/to/lib/MyProject/Entities');
2631
$config->setMetadataDriverImpl($driverImpl);
27-
$config->setQueryCacheImpl($cache);
32+
$config->setQueryCache($queryCache);
2833
$config->setProxyDir('/path/to/myproject/lib/MyProject/Proxies');
2934
$config->setProxyNamespace('MyProject\Proxies');
3035
@@ -41,19 +46,22 @@ steps of configuration.
4146
4247
$em = EntityManager::create($connectionOptions, $config);
4348
49+
Doctrine and Caching
50+
--------------------
51+
52+
Doctrine is optimized for working with caches. The main parts in Doctrine
53+
that are optimized for caching are the metadata mapping information with
54+
the metadata cache and the DQL to SQL conversions with the query cache.
55+
These 2 caches require only an absolute minimum of memory yet they heavily
56+
improve the runtime performance of Doctrine.
57+
58+
Doctrine does not bundle its own cache implementation anymore. Instead,
59+
the PSR-6 standard interfaces are used to access the cache. In the examples
60+
in this documentation, Symfony Cache is used as a reference implementation.
61+
4462
.. note::
4563

4664
Do not use Doctrine without a metadata and query cache!
47-
Doctrine is optimized for working with caches. The main
48-
parts in Doctrine that are optimized for caching are the metadata
49-
mapping information with the metadata cache and the DQL to SQL
50-
conversions with the query cache. These 2 caches require only an
51-
absolute minimum of memory yet they heavily improve the runtime
52-
performance of Doctrine. The recommended cache driver to use with
53-
Doctrine is `APC <https://php.net/apc>`_. APC provides you with
54-
an opcode-cache (which is highly recommended anyway) and a very
55-
fast in-memory cache storage that you can use for the metadata and
56-
query caches as seen in the previous code snippet.
5765

5866
Configuration Options
5967
---------------------
@@ -137,39 +145,30 @@ Metadata Cache (***RECOMMENDED***)
137145
.. code-block:: php
138146
139147
<?php
140-
$config->setMetadataCacheImpl($cache);
141-
$config->getMetadataCacheImpl();
148+
$config->setMetadataCache($cache);
149+
$config->getMetadataCache();
142150
143-
Gets or sets the cache implementation to use for caching metadata
151+
Gets or sets the cache adapter to use for caching metadata
144152
information, that is, all the information you supply via
145153
annotations, xml or yaml, so that they do not need to be parsed and
146154
loaded from scratch on every single request which is a waste of
147-
resources. The cache implementation must implement the
148-
``Doctrine\Common\Cache\Cache`` interface.
155+
resources. The cache implementation must implement the PSR-6
156+
``Psr\Cache\CacheItemPoolInterface`` interface.
149157

150158
Usage of a metadata cache is highly recommended.
151159

152-
The recommended implementations for production are:
153-
154-
155-
- ``Doctrine\Common\Cache\ApcCache``
156-
- ``Doctrine\Common\Cache\ApcuCache``
157-
- ``Doctrine\Common\Cache\MemcacheCache``
158-
- ``Doctrine\Common\Cache\XcacheCache``
159-
- ``Doctrine\Common\Cache\RedisCache``
160-
161-
For development you should use the
162-
``Doctrine\Common\Cache\ArrayCache`` which only caches data on a
163-
per-request basis.
160+
For development you should use an array cache like
161+
``Symfony\Component\Cache\Adapter\ArrayAdapter``
162+
which only caches data on a per-request basis.
164163

165164
Query Cache (***RECOMMENDED***)
166165
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167166

168167
.. code-block:: php
169168
170169
<?php
171-
$config->setQueryCacheImpl($cache);
172-
$config->getQueryCacheImpl();
170+
$config->setQueryCache($cache);
171+
$config->getQueryCache();
173172
174173
Gets or sets the cache implementation to use for caching DQL
175174
queries, that is, the result of a DQL parsing process that includes
@@ -181,18 +180,9 @@ minimal memory usage in your cache).
181180

182181
Usage of a query cache is highly recommended.
183182

184-
The recommended implementations for production are:
185-
186-
187-
- ``Doctrine\Common\Cache\ApcCache``
188-
- ``Doctrine\Common\Cache\ApcuCache``
189-
- ``Doctrine\Common\Cache\MemcacheCache``
190-
- ``Doctrine\Common\Cache\XcacheCache``
191-
- ``Doctrine\Common\Cache\RedisCache``
192-
193-
For development you should use the
194-
``Doctrine\Common\Cache\ArrayCache`` which only caches data on a
195-
per-request basis.
183+
For development you should use an array cache like
184+
``Symfony\Component\Cache\Adapter\ArrayAdapter``
185+
which only caches data on a per-request basis.
196186

197187
SQL Logger (***Optional***)
198188
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -269,10 +259,10 @@ Development vs Production Configuration
269259

270260
You should code your Doctrine2 bootstrapping with two different
271261
runtime models in mind. There are some serious benefits of using
272-
APC or Memcache in production. In development however this will
262+
APCu or Memcache in production. In development however this will
273263
frequently give you fatal errors, when you change your entities and
274264
the cache still keeps the outdated metadata. That is why we
275-
recommend the ``ArrayCache`` for development.
265+
recommend an array cache for development.
276266

277267
Furthermore you should have the Auto-generating Proxy Classes
278268
option to true in development and to false in production. If this

0 commit comments

Comments
 (0)