@@ -20,6 +20,8 @@ are independent from the actual cache implementation. Therefore, applications
2020can keep using the same cache pool even if the underlying cache mechanism
2121changes from a file system based cache to a Redis or database based cache.
2222
23+ .. _component-cache-creating-cache-pools :
24+
2325Creating Cache Pools
2426--------------------
2527
@@ -124,8 +126,9 @@ when all items are successfully deleted)::
124126
125127.. tip ::
126128
127- If the Cache component is used inside a Symfony application, you can remove
128- all the items of a given cache pool with the following command:
129+ If the cache component is used inside a Symfony application, you can remove
130+ *all items * from the *given pool(s) * using the following command (which resides within
131+ the :ref: `framework bundle <framework-bundle-configuration >`):
129132
130133 .. code-block :: terminal
131134
@@ -137,4 +140,64 @@ when all items are successfully deleted)::
137140 # clears the "cache.validation" and "cache.app" pool
138141 $ php bin/console cache:pool:clear cache.validation cache.app
139142
140- .. _`Doctrine Cache` : https://github.com/doctrine/cache
143+ .. _component-cache-cache-pool-prune :
144+
145+ Pruning Cache Items
146+ -------------------
147+
148+ Some cache pools do not include an automated mechanism for pruning expired cache items.
149+ For example, the :ref: `FilesystemAdaper <component-cache-filesystem-adapter >` cache
150+ does not remove expired cache items *until an item is explicitly requested and determined to
151+ be expired *, for example, via a call to :method: `Psr\\ Cache\\ CacheItemPoolInterface::getItem `.
152+ Under certain workloads, this can cause stale cache entries to persist well past their
153+ expiration, resulting in a sizable consumption of wasted disk or memory space from excess,
154+ expired cache items.
155+
156+ This shortcomming has been solved through the introduction of
157+ :class: `Symfony\\ Component\\ Cache\\ PruneableInterface `, which defines the abstract method
158+ :method: `Symfony\\ Component\\ Cache\\ PruneableInterface::prune `. The
159+ :ref: `ChainAdapter <component-cache-chain-adapter >`,
160+ :ref: `FilesystemAdaper <component-cache-filesystem-adapter >`,
161+ :ref: `PdoAdapter <pdo-doctrine-adapter >`, and
162+ :ref: `PhpFilesAdapter <component-cache-files-adapter >` all implement this new interface,
163+ allowing manual removal of stale cache items::
164+
165+ use Symfony\Component\Cache\Adapter\FilesystemAdapter;
166+
167+ $cache = new FilesystemAdapter('app.cache');
168+ // ... do some set and get operations
169+ $cache->prune();
170+
171+ The :ref: `ChainAdapter <component-cache-chain-adapter >` implementation does not directly
172+ contain any pruning logic itself. Instead, when calling the chain adapter's
173+ :method: `Symfony\\ Component\\ Cache\\ ChainAdapter::prune ` method, the call is delegated to all
174+ its compatibe cache adapters (and those that do not implement ``PruneableInterface `` are
175+ silently ignored)::
176+
177+ use Symfony\Component\Cache\Adapter\ApcuAdapter;
178+ use Syfmony\Component\Cache\Adapter\ChainAdapter;
179+ use Syfmony\Component\Cache\Adapter\FilesystemAdapter;
180+ use Syfmony\Component\Cache\Adapter\PdoAdapter;
181+ use Syfmony\Component\Cache\Adapter\PhpFilesAdapter;
182+
183+ $cache = new ChainAdapter(array(
184+ new ApcuAdapter(), // does NOT implement PruneableInterface
185+ new FilesystemAdapter(), // DOES implement PruneableInterface
186+ new PdoAdapter(), // DOES implement PruneableInterface
187+ new PhpFilesAdapter(), // DOES implement PruneableInterface
188+ // ...
189+ ));
190+
191+ // prune will proxy the call to PdoAdapter, FilesystemAdapter and PhpFilesAdapter,
192+ // while silently skipping ApcuAdapter
193+ $cache->prune();
194+
195+ .. tip ::
196+
197+ If the cache component is used inside a Symfony application, you can prune
198+ *all items * from *all pools * using the following command (which resides within
199+ the :ref: `framework bundle <framework-bundle-configuration >`):
200+
201+ .. code-block :: terminal
202+
203+ $ php bin/console cache:pool:prune
0 commit comments