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