The Cache Component
Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
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.
3.3
The PSR-16 "Simple Cache" implementation was introduced in Symfony 3.3.
Installation
1
$ composer require symfony/cache:^3.4
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 exist 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.
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 exist 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
- 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