You are browsing a version that is no longer maintained.

Caching

A Doctrine\DBAL\Statement can automatically cache result sets.

For this to work an instance of Doctrine\Common\Cache\Cache must be provided. This can be set on the configuration object (optionally it can also be passed at query time):

1<?php $cache = new \Doctrine\Common\Cache\ArrayCache(); $config = $conn->getConfiguration(); $config->setResultCacheImpl($cache);
2
3
4

To get the result set of a query cached it is necessary to pass a Doctrine\DBAL\Cache\QueryCacheProfile instance to the executeQuery or executeCacheQuery instance. The difference between these two methods is that the former does not require this instance, while the later has this instance as a required parameter:

1<?php $stmt = $conn->executeQuery($query, $params, $types, new QueryCacheProfile(0, "some key")); $stmt = $conn->executeCacheQuery($query, $params, $types, new QueryCacheProfile(0, "some key"));
2
3

It is also possible to pass in a Doctrine\Common\Cache\Cache instance into the constructor of Doctrine\DBAL\Cache\QueryCacheProfile in which case it overrides the default cache instance:

1<?php $cache = new \Doctrine\Common\Cache\FilesystemCache(__DIR__); new QueryCacheProfile(0, "some key", $cache);
2
3

In order for the data to actually be cached its necessary to ensure that the entire result set is read (the easiest way to ensure this is to use one of the fetchAll*() methods):

1<?php $stmt = $conn->executeCacheQuery($query, $params, $types, new QueryCacheProfile(0, "some key")); $data = $stmt->fetchAllAssociative();
2
3

When using the cache layer not all fetch modes are supported. See the code of the ResultCacheStatement for details.