Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Components
  4. The Cache Component
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Installation
  • Cache (PSR-6) Versus Simple Cache (PSR-16)
  • Simple Caching (PSR-16)
    • Available Simple Cache (PSR-16) Classes
  • More Advanced Caching (PSR-6)
  • Basic Usage (PSR-6)
  • Advanced Usage (PSR-6)

The Cache Component

Edit this page

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

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

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 for performance and resiliency, and ships with ready to use adapters for the most common caching backends, including proxies for adapting from/to Doctrine Cache.

Installation

1
$ composer require symfony/cache

Alternatively, you can clone the https://github.com/symfony/cache repository.

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

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:

1
2
3
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.products_count', 4711);

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

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

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

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

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

// 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([
    'stats.products_count' => 4711,
    'stats.users_count' => 1356,
]);

$stats = $cache->getMultiple([
    'stats.products_count',
    'stats.users_count',
]);

$cache->deleteMultiple([
    'stats.products_count',
    'stats.users_count',
]);

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.

  • ApcuCache
  • ArrayCache
  • ChainCache
  • DoctrineCache
  • FilesystemCache
  • MemcachedCache
  • NullCache
  • PdoCache
  • PhpArrayCache
  • PhpFilesCache
  • RedisCache
  • TraceableCache

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:

1
2
3
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
$productsCount = $cache->getItem('stats.products_count');

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

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

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

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

Advanced Usage (PSR-6)

  • APCu Cache Adapter
  • Array Cache Adapter
  • Chain Cache Adapter
  • Doctrine Cache Adapter
  • Filesystem Cache Adapter
  • Memcached Cache Adapter
  • PDO & Doctrine DBAL Cache Adapter
  • Php Array Cache Adapter
  • Php Files Cache Adapter
  • Proxy Cache Adapter
  • Redis Cache Adapter
  • Cache Invalidation
  • Cache Items
  • Cache Pools and Supported Adapters
  • Adapters For Interoperability between PSR-6 and PSR-16 Cache
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
No stress: we've got you covered with our 116 automated quality checks of your code

No stress: we've got you covered with our 116 automated quality checks of your code

Code consumes server resources. Blackfire tells you how

Code consumes server resources. Blackfire tells you how

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Evan S Kaufman, a Symfony contributor

Thanks Evan S Kaufman (@evanskaufman) for being a Symfony contributor

3 commits • 49 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia