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 in full color showing how to combine Symfony with Docker, APIs, queues & async tasks, Webpack, Single-Page Applications, etc.

Buy printed version
WARNING: You are browsing the documentation for Symfony 3.3 which is not maintained anymore. Consider upgrading your projects to Symfony 5.1.

Chain Cache Adapter

3.3 version

Chain Cache Adapter

This adapter allows to combine any number of the other available cache adapters. Cache items are fetched from the first adapter which contains them and cache items are saved in all the given adapters. This offers a simple way of creating a layered cache.

This adapter expects an array of adapters as its first parameter, and optionally a maximum cache lifetime as its second parameter:

use Symfony\Component\Cache\Adapter\ApcuAdapter;

$cache = new ChainAdapter(array(

    // The ordered list of adapters used to fetch cached items
    array $adapters,

    // The max lifetime of items propagated from lower adapters to upper ones
    $maxLifetime = 0
));

Note

When an item is not found in the first adapter but is found in the next ones, this adapter ensures that the fetched item is saved in all the adapters where it was previously missing.

The following example shows how to create a chain adapter instance using the fastest and slowest storage engines, Symfony\Component\Cache\Adapter\ApcuAdapter and Symfony\Component\Cache\Adapter\FilesystemAdapter, respectfully:

use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new ChainAdapter(array(
    new ApcuAdapter(),
    new FilesystemAdapter(),
));

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