English spoken conference
WARNING: You are browsing the documentation for Symfony 4.0 which is not maintained anymore. Consider upgrading your projects to Symfony 4.3.

You are browsing the Symfony 4.0 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4.0 yet, browse the Symfony 3.4 documentation.

Php Array Cache Adapter

4.0 version

Php Array Cache AdapterΒΆ

This adapter is a high performance cache for static data (e.g. application configuration) that is optimized and preloaded into OPcache memory storage:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

// somehow, decide it's time to warm up the cache!
if ($needsWarmup) {
    // some static values
    $values = array(
        'stats.products_count' => 4711,
        'stats.users_count' => 1356,
    );

    $cache = new PhpArrayAdapter(
        // single file where values are cached
        __DIR__ . '/somefile.cache',
        // a backup adapter, if you set values after warmup
        new FilesystemAdapter()
    );
    $cache->warmUp($values);
}

// ... then, use the cache!
$cacheItem = $cache->getItem('stats.users_count');
echo $cacheItem->get();

Note

This adapter requires PHP 7.x and should be used with the php.ini setting opcache.enable on.

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