PDO Cache Adapter
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, options array and marshaller as the other optional arguments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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 = [],
// an optional marshaller object used to serialize the cache items before storing them
$marshaller = null
);
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.
Working with Tags
8.2
The PdoTagAwareAdapter and the cache.adapter.pdo_tag_aware service
were introduced in Symfony 8.2.
The PdoTagAwareAdapter takes
the same arguments as PdoAdapter and adds native support for
cache tags. It stores the tags in a second table
(one row per item and tag, with an index on the tag column), so invalidating
tags runs a single DELETE query:
1 2 3 4 5 6 7 8 9 10 11
use Symfony\Component\Cache\Adapter\PdoTagAwareAdapter;
$cache = new PdoTagAwareAdapter($databaseConnectionOrDSN);
$item = $cache->getItem('cache_key');
$item->set('cache_value');
$item->tag(['tag_1', 'tag_2']);
$cache->save($item);
// deletes all the items tagged with "tag_1"
$cache->invalidateTags(['tag_1']);
Use this adapter instead of wrapping PdoAdapter with the
TagAwareAdapter, which stores
the tag versions as regular cache items and needs extra queries to check them
when reading and saving tagged items.
Both tables are created automatically when they are first needed. Unlike
PdoAdapter, this adapter doesn't provide a method to create them explicitly.
Invalidating tags deletes the tagged items but keeps their rows in the tags
table. Call the prune() method to also delete the rows of the tags table
that don't belong to any existing item.
In addition to the options of PdoAdapter, this adapter defines the following
options to configure the tags table:
| Option | Default value |
|---|---|
db_tags_table |
cache_tags |
db_tags_col |
item_tag |
db_tags_tag_index_name |
idx_cache_tags_item_tag |
In Symfony applications, use the cache.adapter.pdo_tag_aware adapter. Pools
based on it support tags on their own, so you don't need to set their tags
option. The provider of the pool defaults to the value of the
default_pdo_provider option, which has no default value, so you must set
one of them:
1 2 3 4 5 6 7
# config/packages/cache.yaml
framework:
cache:
pools:
my_cache_pool:
adapter: cache.adapter.pdo_tag_aware
provider: 'pgsql:host=localhost'