Symfony 4 was released on November 30th.
Update now to the best Symfony ever!

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

The Cache Component

4.0 version
Maintained Unmaintained

The Cache Component

The Cache component provides an extended PSR-6 implementation as well as a PSR-16 "Simple Cache" implementation for adding cache to your applications. It is designed to have a low overhead and it ships with ready to use adapters for the most common caching backends.

Installation

You can install the component in 2 different ways:

Cache (PSR-6) Versus Simple Cache (PSR-16)

This component includes two different approaches to caching:

PSR-6 Caching:
A fully-featured cache system, which includes cache "pools", more advanced cache "items", and cache tagging for invalidation.
PSR-16 Simple Caching:
A simple way to store, fetch and remove items from a cache.

Both methods support the same cache adapters and will give you very similar performance.

Tip

The component also contains adapters to convert between PSR-6 and PSR-16 caches. See Adapters For Interoperability between PSR-6 and PSR-16 Cache.

Simple Caching (PSR-16)

This part of the component is an implementation of PSR-16, which means that its basic API is the same as defined in the standard. First, create a cache object from one of the built-in cache classes. For example, to create a filesystem-based cache, instantiate FilesystemCache:

use Symfony\Component\Cache\Simple\FilesystemCache;

$cache = new FilesystemCache();

Now you can create, retrieve, update and delete items using this object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// save a new item in the cache
$cache->set('stats.num_products', 4711);

// or set it with a custom ttl
// $cache->set('stats.num_products', 4711, 3600);

// retrieve the cache item
if (!$cache->has('stats.num_products')) {
    // ... item does not exists in the cache
}

// retrieve the value stored by the item
$numProducts = $cache->get('stats.num_products');

// or specify a default value, if the key doesn't exist
// $numProducts = $cache->get('stats.num_products', 100);

// remove the cache key
$cache->delete('stats.num_products');

// clear *all* cache keys
$cache->clear();

You can also work with multiple items at once:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$cache->setMultiple(array(
    'stats.num_products' => 4711,
    'stats.num_users' => 1356,
));

$stats = $cache->getMultiple(array(
    'stats.num_products',
    'stats.num_users',
));

$cache->deleteMultiple(array(
    'stats.num_products',
    'stats.num_users',
));

Available Simple Cache (PSR-16) Classes

The following cache adapters are available:

Tip

To find out more about each of these classes, you can read the PSR-6 Cache Pool page. These "Simple" (PSR-16) cache classes aren't identical to the PSR-6 Adapters on that page, but each share constructor arguments and use-cases.

More Advanced Caching (PSR-6)

To use the more-advanced, PSR-6 Caching abilities, you'll need to learn its key concepts:

Item
A single unit of information stored as a key/value pair, where the key is the unique identifier of the information and the value is its contents;
Pool
A logical repository of cache items. All cache operations (saving items, looking for items, etc.) are performed through the pool. Applications can define as many pools as needed.
Adapter
It implements the actual caching mechanism to store the information in the filesystem, in a database, etc. The component provides several ready to use adapters for common caching backends (Redis, APCu, Doctrine, PDO, etc.)

Basic Usage (PSR-6)

This part of the component is an implementation of PSR-6, which means that its basic API is the same as defined in the standard. Before starting to cache information, create the cache pool using any of the built-in adapters. For example, to create a filesystem-based cache, instantiate FilesystemAdapter:

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new FilesystemAdapter();

Now you can create, retrieve, update and delete items using this cache pool:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// create a new item by trying to get it from the cache
$numProducts = $cache->getItem('stats.num_products');

// assign a value to the item and save it
$numProducts->set(4711);
$cache->save($numProducts);

// retrieve the cache item
$numProducts = $cache->getItem('stats.num_products');
if (!$numProducts->isHit()) {
    // ... item does not exists in the cache
}
// retrieve the value stored by the item
$total = $numProducts->get();

// remove the cache item
$cache->deleteItem('stats.num_products');

For a list of all of the supported adapters, see Cache Pools and Supported Adapters.

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