@@ -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
@@ -144,24 +146,51 @@ Pruning Cache Items
144146
145147.. versionadded :: 3.4
146148
147- Pruning functionality was introduced in Symfony 3.4.
148-
149- Some cache pools do not include an automated mechanism for pruning expired cache
150- items. For example, :class: `Symfony\\ Component\\ Cache\\ Adapter\\ FilesystemAdapter `
151- does not remove expired items until one is explicitly requested and determined to
152- be expired, for example, via a call to :method: `Psr\\ Cache\\ CacheItemPoolInterface::getItem `.
153- This may result in many stale cache entries persisting well past their expiration,
154- causing a build up that can result in a sizable consumption of wasted disk or memory
155- space.
156-
157- This concern has been resolved through the introduction of
158- :class: `Symfony\\ Component\\ Cache\\ PruneableInterface `, an interface defining a
159- singular ``prune() `` method. Relevant cache adapters, those that allow expired cache
160- entries to proliferate, now implement ``PruneableInterface ``, allowing for these
161- excess cache entries to be manually removed::
149+ Cache adapter pruning functionality was introduced in Symfony 3.4.
150+
151+ Some cache pools do not include an automated mechanism for pruning expired cache items.
152+ For example, the :ref: `FilesystemAdaper <component-cache-filesystem-adapter >` cache
153+ does not remove expired cache items *until an item is explicitly requested and determined to
154+ be expired *, for example, via a call to :method: `Psr\\ Cache\\ CacheItemPoolInterface::getItem `.
155+ Under certain workloads, this can cause stale cache entries to persist well past their
156+ expiration, resulting in a sizable consumption of wasted disk or memory space from
157+ excess, expired cache items.
158+
159+ This shortcomming has been solved through the introduction of
160+ :class: `Symfony\\ Component\\ Cache\\ PruneableInterface `, which defines the abstract method
161+ :method: `Symfony\\ Component\\ Cache\\ PruneableInterface::prune `. The
162+ :ref: `ChainAdapter <component-cache-chain-adapter >`,
163+ :ref: `FilesystemAdaper <component-cache-filesystem-adapter >`,
164+ :ref: `PdoAdapter <pdo-doctrine-adapter >`, and
165+ :ref: `PhpFilesAdapter <component-cache-files-adapter >` all implement this new interface,
166+ allowing manual removal of stale cache items::
162167
163168 use Symfony\Component\Cache\Adapter\FilesystemAdapter;
164169
165170 $cache = new FilesystemAdapter('app.cache');
166171 // ... do some set and get operations
167172 $cache->prune();
173+
174+ The :ref: `ChainAdapter <component-cache-chain-adapter >` implementation does not directly
175+ contain any pruning logic itself. Instead, when calling the chain adapter's
176+ :method: `Symfony\\ Component\\ Cache\\ ChainAdapter::prune ` method, the call is deligated to all
177+ its compatibe cache adapters (and those that do not implement ``PruneableInterface `` are
178+ silently ignored)::
179+
180+ use Symfony\Component\Cache\Adapter\ApcuAdapter;
181+ use Syfmony\Component\Cache\Adapter\ChainAdapter;
182+ use Syfmony\Component\Cache\Adapter\FilesystemAdapter;
183+ use Syfmony\Component\Cache\Adapter\PdoAdapter;
184+ use Syfmony\Component\Cache\Adapter\PhpFilesAdapter;
185+
186+ $cache = new ChainAdapter(array(
187+ new ApcuAdapter(), // does NOT implement PruneableInterface
188+ new FilesystemAdapter(), // DOES implement PruneableInterface
189+ new PdoAdapter(), // DOES implement PruneableInterface
190+ new PhpFilesAdapter(), // DOES implement PruneableInterface
191+ // ...
192+ ));
193+
194+ // prune will proxy the call to PdoAdapter, FilesystemAdapter and PhpFilesAdapter,
195+ // while silently skipping ApcuAdapter
196+ $cache->prune();
0 commit comments