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.

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

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

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.

Cache Pools include methods to delete a cache item, some of them or all of them.
The most common is Psr\\Cache\\CacheItemPoolInterface::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 Psr\\Cache\\CacheItemPoolInterface::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(['category1','category2']);

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

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

Tip

If the cache component is used inside a Symfony application, you can remove
items from cache pools using the following commands (which reside within
the framework bundle):

Some cache pools do not include an automated mechanism for pruning expired cache items.
For example, the FilesystemAdapter cache
does not remove expired cache items until an item is explicitly requested and determined to
be expired, for example, via a call to Psr\\Cache\\CacheItemPoolInterface::getItem.
Under certain workloads, this can cause stale cache entries to persist well past their
expiration, resulting in a sizable consumption of wasted disk or memory space from excess,
expired cache items.

useSymfony\Component\Cache\Adapter\FilesystemAdapter;$cache=newFilesystemAdapter('app.cache');// ... do some set and get operations$cache->prune();

The ChainAdapter implementation does not directly
contain any pruning logic itself. Instead, when calling the chain adapter's
prune() method, the call is delegated to all
its compatible cache adapters (and those that do not implement PruneableInterface are
silently ignored):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

useSymfony\Component\Cache\Adapter\ApcuAdapter;useSymfony\Component\Cache\Adapter\ChainAdapter;useSymfony\Component\Cache\Adapter\FilesystemAdapter;useSymfony\Component\Cache\Adapter\PdoAdapter;useSymfony\Component\Cache\Adapter\PhpFilesAdapter;$cache=newChainAdapter([newApcuAdapter(),// does NOT implement PruneableInterfacenewFilesystemAdapter(),// DOES implement PruneableInterfacenewPdoAdapter(),// DOES implement PruneableInterfacenewPhpFilesAdapter(),// DOES implement PruneableInterface// ...]);// prune will proxy the call to PdoAdapter, FilesystemAdapter and PhpFilesAdapter,// while silently skipping ApcuAdapter$cache->prune();

Tip

If the cache component is used inside a Symfony application, you can prune
all items from all pools using the following command (which resides within
the framework bundle):