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.

Php Array Cache Adapter

3.3 version

Php Array Cache Adapter

This adapter is a highly performant way to cache static data (e.g. application configuration) that is optimized and preloaded into OPcache memory storage:

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.num_products' => 4711,
        'stats.num_users' => 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.num_users');
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.