PDO & Doctrine DBAL Cache Adapter

PDO & Doctrine DBAL Cache Adapter

This adapter stores the cache items in an SQL database. It requires a [PDO](https://www.php.net/manual/en/class.pdo.php "PDO"), Doctrine DBAL Connection, or Data Source Name (DSN) as its first parameter, and optionally a namespace, default cache lifetime, and options array as its second, third, and forth parameters:

  1. use Symfony\Component\Cache\Adapter\PdoAdapter;
  2. $cache = new PdoAdapter(
  3. // a PDO, a Doctrine DBAL connection or DSN for lazy connecting through PDO
  4. $databaseConnectionOrDSN,
  5. // the string prefixed to the keys of the items stored in this cache
  6. $namespace = '',
  7. // the default lifetime (in seconds) for cache items that do not define their
  8. // own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
  9. // until the database table is truncated or its rows are otherwise deleted)
  10. $defaultLifetime = 0,
  11. // an array of options for configuring the database table and connection
  12. $options = []
  13. );

The table where values are stored is created automatically on the first call to the [save()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Cache/Adapter/PdoAdapter.php "Symfony\Component\Cache\Adapter\PdoAdapter::save()") method. You can also create this table explicitly by calling the [createTable()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Cache/Adapter/PdoAdapter.php "Symfony\Component\Cache\Adapter\PdoAdapter::createTable()") method in your code.

Tip

When passed a Data Source Name (DSN) string (instead of a database connection class instance), the connection will be lazy-loaded when needed.

Note

This adapter implements Symfony\Component\Cache\PruneableInterface, allowing for manual pruning of expired cache entries by calling its prune() method.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.