Skip to content

PDO Cache Adapter

Warning: You are browsing the documentation for Symfony 7.0, which is no longer maintained.

Read the updated version of this page for Symfony 7.1 (the current stable version).

The PDO adapters store the cache items in a table of an SQL database.

Note

This adapter implements PruneableInterface, allowing for manual pruning of expired cache entries by calling the prune() method.

The PdoAdapter requires a PDO, or DSN as its first parameter. You can pass a namespace, default cache lifetime, and options array as the other optional arguments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 save() method. You can also create this table explicitly by calling the 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.

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