Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.


You are browsing the documentation for Symfony 3.3 which is not maintained anymore.

Consider upgrading your projects to Symfony 5.2.

Cache Pools and Supported Adapters

3.3 version

Cache Pools and Supported Adapters

Cache Pools are the logical repositories of cache items. They perform all the common operations on items, such as saving them or looking for them. Cache pools are independent from the actual cache implementation. Therefore, applications can keep using the same cache pool even if the underlying cache mechanism changes from a file system based cache to a Redis or database based cache.

Creating Cache Pools

Cache Pools are created through the cache adapters, which are classes that implement Symfony\Component\Cache\Adapter\AdapterInterface. This component provides several adapters ready to use in your applications.

Looking for Cache Items

Cache Pools define three methods to look for cache items. The most common method is getItem($key), which returns the cache item identified by the given key:

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new FilesystemAdapter('app.cache');
$latestNews = $cache->getItem('latest_news');

If no item is defined for the given key, the method doesn’t return a null value but an empty object which implements the Symfony\Component\Cache\CacheItem class.

If you need to fetch several cache items simultaneously, use instead the getItems(array($key1, $key2, ...)) method:

// ...
$stocks = $cache->getItems(array('AAPL', 'FB', 'GOOGL', 'MSFT'));

Again, if any of the keys doesn’t represent a valid cache item, you won’t get a null value but an empty CacheItem object.

The last method related to fetching cache items is hasItem($key), which returns true if there is a cache item identified by the given key:

// ...
$hasBadges = $cache->hasItem('user_'.$userId.'_badges');

Saving Cache Items

The most common method to save cache items is save(), which stores the item in the cache immediately (it returns true if the item was saved or false if some error occurred):

// ...
$userFriends = $cache->getItem('user_'.$userId.'_friends');
$isSaved = $cache->save($userFriends);

Sometimes you may prefer to not save the objects immediately in order to increase the application performance. In those cases, use the saveDeferred() method to mark cache items as “ready to be persisted” and then call to commit() method when you are ready to persist them all:

// ...
$isQueued = $cache->saveDeferred($userFriends);
// ...
$isQueued = $cache->saveDeferred($userPreferences);
// ...
$isQueued = $cache->saveDeferred($userRecentProducts);
// ...
$isSaved = $cache->commit();

The saveDeferred() method returns true when the cache item has been successfully added to the “persist queue” and false otherwise. The commit() method returns true when all the pending items are successfully saved or false otherwise.

Removing Cache Items

Cache Pools include methods to delete a cache item, some of them or all of them. The most common is deleteItem(), which deletes the cache item identified by the given key (it returns true when the item is successfully deleted or doesn’t exist and false otherwise):

// ...
$isDeleted = $cache->deleteItem('user_'.$userId);

Use the deleteItems() method to delete several cache items simultaneously (it returns true only if all the items have been deleted, even when any or some of them don’t exist):

// ...
$areDeleted = $cache->deleteItems(array('category1', 'category2'));

Finally, to remove all the cache items stored in the pool, use the clear() method (which returns true when all items are successfully deleted):

// ...
$cacheIsEmpty = $cache->clear();


If the Cache component is used inside a Symfony application, you can remove all the items of a given cache pool with the following command:

$ php bin/console cache:pool:clear <cache-pool-name>

# clears the "" pool
$ php bin/console cache:pool:clear

# clears the "cache.validation" and "" pool
$ php bin/console cache:pool:clear cache.validation

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