The Cache Component
The Cache Component¶
New in version 3.3: The PSR-16 “Simple Cache” implementation was introduced in Symfony 3.3.
Installation¶
You can install the component in 2 different ways:
- Install it via Composer (
symfony/cache
on Packagist); - Use the official Git repository (https://github.com/symfony/cache).
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 Symfony\Component\Cache\Simple\FilesystemCache
:
use Symfony\Component\Cache\Simple\FilesystemCache;
$cache = new FilesystemCache();
Now you can create, retrieve, update and delete items using this object:
// 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:
$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.
Symfony\Component\Cache\Simple\ApcuCache
Symfony\Component\Cache\Simple\ArrayCache
Symfony\Component\Cache\Simple\ChainCache
Symfony\Component\Cache\Simple\DoctrineCache
Symfony\Component\Cache\Simple\FilesystemCache
Symfony\Component\Cache\Simple\MemcachedCache
Symfony\Component\Cache\Simple\NullCache
Symfony\Component\Cache\Simple\PdoCache
Symfony\Component\Cache\Simple\PhpArrayCache
Symfony\Component\Cache\Simple\PhpFilesCache
Symfony\Component\Cache\Simple\RedisCache
Symfony\Component\Cache\Simple\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 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
$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.