Doctrine\DBAL\Connection can automatically cache result sets. The
feature is optional though, and by default, no result set is cached.
To use the result cache, there are three mandatory steps:
- Configure a global result cache, or provide one at query time.
- Provide a cache profile for the result set you want to cache when making a query.
Any instance of
Psr\Cache\CacheItemPoolInterface can be used as a result
cache and can be set on the configuration object (optionally it can also
be passed at query time):
Note that this documentation uses Symfony Cache in all examples. Any other cache implementation that follows the PSR-6 standard can be used instead.
To get the result set of a query cached, it is necessary to pass a
Doctrine\DBAL\Cache\QueryCacheProfile instance to the
executeCacheQuery() methods. The difference
between these two methods is that the former has the cache profile as an
optional argument, whereas it is required when calling the latter:
As stated before, it is also possible to pass in a
Psr\Cache\CacheItemPoolInterface instance into the constructor of
Doctrine\DBAL\Cache\QueryCacheProfile in which case it overrides the
default cache instance: