Chain Cache Adapter

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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, ApcuAdapter and FilesystemAdapter, respectfully:

1
2
3
4
5
6
7
8
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(),
));

Эта документация является переводом официальной документации Symfony и предоставляется по свободной лицензии CC BY-SA 3.0.