Warning: You are browsing the documentation for Symfony 3.1, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

The Cache Component

3.1 version

The Cache Component

The Cache component provides a strict PSR-6 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.

New in version 3.1: The Cache component was introduced in Symfony 3.1.


You can install the component in 2 different ways:

Key Concepts

Before starting to use the Cache component, it’s important that you learn the meaning of some key concepts:

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;
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.
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, etc.)

Basic Usage

This component is a strict implementation of PSR-6, which means that the 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 Symfony\Component\Cache\Adapter\FilesystemAdapter:

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new FilesystemAdapter();

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

// 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

// 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

Advanced Usage

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