Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

New in Symfony 3.2: Cache improvements

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

Added a new "Null" adapter

Contributed by
Titouan Galopin
in #18825.

The new Symfony\Component\Cache\Adapter\NullAdapter is a special cache adapter that disables the cache (it loses all the items saved on it and it returns false for all the read and save operations). This adapter is mostly useful for tests.

Added a faster file system adapter

The new Symfony\Component\Cache\Adapter\PhpFilesAdapter is very similar to the existing FilesystemAdapter but it has a better performance when using OPcache in the server.

The trick is that when items are saved, the new adapter creates a PHP file that is included during the fetch() operations. This allows OPcache to cache those files in its memory. In our benchmarks, PhpFilesAdapter was 3.5 times faster than FilesystemAdapter for read operations but a bit slower for write operations, so it's mostly meant for data that rarely changes.

Added a new PDO and Doctrine DBAL adapter

Contributed by
Nicolas Grekas
in #19519.

The new Symfony\Component\Cache\Adapter\PdoAdapter allows to use any DBAL-compatible database as the storage of your cache. Its implementation heavily borrows from PdoSessionHandler. For example, to create a SQLite-based cache, execute the following:

use Symfony\Component\Cache\Adapter\PdoAdapter;

$dbFilePath = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
$pool = new PdoAdapter('sqlite:'.$dbFilePath);

Added a generic tag-aware adapter

Contributed by
Nicolas Grekas
in #19524.

Tag-based invalidation is one of the mechanisms provided by Symfony to invalidate caches, which is the process of removing all cached items related to a change in the state of your model.

In Symfony 3.2, a new Symfony\Component\Cache\Adapter\TagAwareAdapter class and Symfony\Component\Cache\Adapter\TagAwareAdapterInterface interface allow to transform any cache adapter into a tag-aware adapter.

The constructor of the TagAwareAdapter class accepts two arguments: the first one is the cache adapter used to store the items and the second optional argument is the adapter used to cache the tags. This allows you for example to use a file system or database cache for storing large items and to use a Redis cache for the tags to have ultra-fast invalidation checks:

use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;

$cache = new TagAwareAdapter(
    new FilesystemAdapter(),
    new RedisAdapter('redis://localhost')

Added a command to clear cache pools

Contributed by
Nicolas Grekas
in #19891.

The FrameworkBundle now includes a cache:pool:clear command that takes as argument one or more service names. These services are the cache pools to clear or the Cache clearer services to invoke:

$ ./bin/console cache:pool:clear app.cache app.cache.products
Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.


The new Symfony\Component\Cache\Adapter\PdoAdapter.php

you should remove the *.php extension :)
And double "use" too
use use Symfony\Component\Cache\Adapter\PdoAdapter;
@Oskar @Ivan I've fixed all those typos. Thanks!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.