PDO & Doctrine DBAL Cache Adapter
The PDO and Doctrine DBAL adapters store the cache items in a table of an SQL database.
Note
These adapters implement Symfony\Component\Cache\PruneableInterface,
allowing for manual pruning of expired cache entries
by calling the prune() method.
Using PHP PDO
The Symfony\Component\Cache\Adapter\PdoAdapter requires a :phpclass:`PDO`,
or Data Source Name (DSN) as its first parameter. You can pass a namespace,
default cache lifetime, and options array as the other optional arguments:
use Symfony\Component\Cache\Adapter\PdoAdapter;
$cache = new PdoAdapter(
// a PDO connection or DSN for lazy connecting through PDO
$databaseConnectionOrDSN,
// the string prefixed to the keys of the items stored in this cache
$namespace = '',
// the default lifetime (in seconds) for cache items that do not define their
// own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
// until the database table is truncated or its rows are otherwise deleted)
$defaultLifetime = 0,
// an array of options for configuring the database table and connection
$options = []
);
The table where values are stored is created automatically on the first call to the :method:`Symfony\\Component\\Cache\\Adapter\\PdoAdapter::save` method. You can also create this table explicitly by calling the :method:`Symfony\\Component\\Cache\\Adapter\\PdoAdapter::createTable` method in your code.
Deprecated since version 5.4: Using Symfony\Component\Cache\Adapter\PdoAdapter with a
Doctrine\DBAL\Connection or a DBAL URL is deprecated since Symfony 5.4
and will be removed in Symfony 6.0.
Use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter instead.
Tip
When passed a Data Source Name (DSN) string (instead of a database connection class instance), the connection will be lazy-loaded when needed. DBAL Connection are lazy-loaded by default; some additional options may be necessary to detect the database engine and version without opening the connection.
Using Doctrine DBAL
The Symfony\Component\Cache\Adapter\DoctrineDbalAdapter requires a
Doctrine DBAL Connection, or Doctrine DBAL URL as its first parameter.
You can pass a namespace, default cache lifetime, and options array as the other
optional arguments:
use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;
$cache = new DoctrineDbalAdapter(
// a Doctrine DBAL connection or DBAL URL
$databaseConnectionOrURL,
// the string prefixed to the keys of the items stored in this cache
$namespace = '',
// the default lifetime (in seconds) for cache items that do not define their
// own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
// until the database table is truncated or its rows are otherwise deleted)
$defaultLifetime = 0,
// an array of options for configuring the database table and connection
$options = []
);
Note
DBAL Connection are lazy-loaded by default; some additional options may be necessary to detect the database engine and version without opening the connection.