Skip to content

Commit e185491

Browse files
committed
cleanup clear
1 parent 188c97d commit e185491

File tree

8 files changed

+65
-24
lines changed

8 files changed

+65
-24
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ Changelog
33

44
See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpCache/releases).
55

6+
2.6.0 (unreleased)
7+
------------------
8+
9+
### Cache Clear
10+
11+
* Added: ClearCapable to clear the whole cache in one efficient call. Currently
12+
supported only by the Symfony HttpCache.
13+
614
2.5.4
715
-----
816

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
},
5858
"extra": {
5959
"branch-alias": {
60-
"dev-master": "2.5.x-dev"
60+
"dev-master": "2.6.x-dev"
6161
}
6262
}
6363
}

doc/invalidation-introduction.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,5 @@ all methods, please refer to proxy specific documentation for the details.
151151

152152
Clear
153153
Clearing a cache means removing all its cache entries completely. It can be
154-
used for a more efficient cache reset rather than banning or purging every
155-
URL individually.
154+
used for a more efficient cache reset rather than a ban that matches every
155+
request or purging every URL individually.

doc/proxy-clients.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ Multiplexer ✓ ✓ ✓ ✓
3131

3232
(1): Only when using `Toflar Psr6Store`_.
3333

34-
35-
Of course, you can also implement your own client for other needs. Have a look
34+
If needed, you can also implement your own client for other needs. Have a look
3635
at the interfaces in namespace ``FOS\HttpCache\ProxyClient\Invalidation``.
3736

37+
"Clear" can be emulated by "Ban" with a request that matches everything. If
38+
both are available, "Clear" is preferred as it can be implemented by the
39+
caching proxy more efficiently.
40+
3841
.. _client setup:
3942

4043
Setup
@@ -318,11 +321,11 @@ Implementation Notes
318321
--------------------
319322

320323
Each client is an implementation of :source:`ProxyClient <src/ProxyClient/ProxyClient.php>`.
321-
All other interfaces, ``PurgeCapable``, ``RefreshCapable``, ``BanCapable``, ``ClearCapable`` and
322-
``TagCapable``, extend this ``ProxyClient``. So each client implements at least
323-
one of the three :ref:`invalidation methods <invalidation methods>` depending on
324+
All other interfaces, ``PurgeCapable``, ``RefreshCapable``, ``BanCapable``, ``TagCapable``
325+
and ``ClearCapable`` extend this ``ProxyClient``. So each client implements at least
326+
one of the :ref:`invalidation methods <invalidation methods>` depending on
324327
the proxy server’s abilities. To interact with a proxy client directly, refer to
325-
the doc comments on the interfaces.
328+
the phpdoc on the interfaces.
326329

327330
The ``ProxyClient`` has one method: ``flush()``. After collecting
328331
invalidation requests, ``flush()`` needs to be called to actually send the

src/ProxyClient/Invalidation/ClearCapable.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,16 @@
1414
use FOS\HttpCache\ProxyClient\ProxyClient;
1515

1616
/**
17-
* An HTTP cache that supports clearing all of its
18-
* cache entries.
17+
* An HTTP cache that supports removing all of its cache entries.
1918
*
20-
* Proxy clients supporting this interface can allow
21-
* for a more efficient delete-all operation rather
22-
* than banning everything. Also it serves as
23-
* an alternative to proxies that do not support
24-
* banning cache entries at all.
19+
* This operation allows to clear proxies that do not support banning.
20+
* Additionally, this operation is likely more efficient than a ban request
21+
* that matches everything.
2522
*/
2623
interface ClearCapable extends ProxyClient
2724
{
2825
/**
29-
* Clear the cache completely.
26+
* Remove all cache items from this cache.
3027
*
3128
* @return $this
3229
*/

src/ProxyClient/MultiplexerClient.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use FOS\HttpCache\Exception\ExceptionCollection;
1515
use FOS\HttpCache\Exception\InvalidArgumentException;
1616
use FOS\HttpCache\ProxyClient\Invalidation\BanCapable;
17+
use FOS\HttpCache\ProxyClient\Invalidation\ClearCapable;
1718
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
1819
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
1920
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
@@ -23,7 +24,7 @@
2324
*
2425
* @author Emanuele Panzeri <[email protected]>
2526
*/
26-
class MultiplexerClient implements BanCapable, PurgeCapable, RefreshCapable, TagCapable
27+
class MultiplexerClient implements BanCapable, PurgeCapable, RefreshCapable, TagCapable, ClearCapable
2728
{
2829
/**
2930
* @var ProxyClient[]
@@ -143,6 +144,18 @@ public function refresh($url, array $headers = [])
143144
return $this;
144145
}
145146

147+
/**
148+
* Forwards to all clients.
149+
*
150+
* @return $this
151+
*/
152+
public function clear()
153+
{
154+
$this->invoke(ClearCapable::class, 'clear', []);
155+
156+
return $this;
157+
}
158+
146159
/**
147160
* Invoke the given $method on all available ProxyClients implementing the
148161
* given $interface.

src/ProxyClient/Symfony.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ protected function configureOptions()
7474
}
7575

7676
/**
77-
* Remove/Expire cache objects based on cache tags.
78-
*
79-
* @param array $tags Tags that should be removed/expired from the cache
80-
*
81-
* @return $this
77+
* {@inheritdoc}
8278
*/
8379
public function invalidateTags(array $tags)
8480
{
@@ -99,7 +95,10 @@ public function invalidateTags(array $tags)
9995
}
10096

10197
/**
102-
* Clear the cache completely.
98+
* {@inheritdoc}
99+
*
100+
* Clearing the cache is implemented with a purge request with a special
101+
* header to indicate that the whole cache should be removed.
103102
*
104103
* @return $this
105104
*/
@@ -110,5 +109,7 @@ public function clear()
110109
[$this->options['clear_cache_header'] => 'true'],
111110
false
112111
);
112+
113+
return $this;
113114
}
114115
}

tests/Unit/ProxyClient/MultiplexerClientTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOS\HttpCache\Tests\Unit\ProxyClient;
1313

1414
use FOS\HttpCache\ProxyClient\Invalidation\BanCapable;
15+
use FOS\HttpCache\ProxyClient\Invalidation\ClearCapable;
1516
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
1617
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
1718
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
@@ -134,6 +135,7 @@ public function testPurge()
134135
->getMock();
135136
$mockClient2 = \Mockery::mock(PurgeCapable::class)
136137
->shouldReceive('purge')
138+
->once()
137139
->with($url, $headers)
138140
->getMock();
139141
$mockClient3 = \Mockery::mock(ProxyClient::class);
@@ -143,6 +145,23 @@ public function testPurge()
143145
$this->assertSame($multiplexer, $multiplexer->purge($url, $headers));
144146
}
145147

148+
public function testClear()
149+
{
150+
$mockClient1 = \Mockery::mock(ClearCapable::class)
151+
->shouldReceive('clear')
152+
->once()
153+
->getMock();
154+
$mockClient2 = \Mockery::mock(ClearCapable::class)
155+
->shouldReceive('clear')
156+
->once()
157+
->getMock();
158+
$mockClient3 = \Mockery::mock(ProxyClient::class);
159+
160+
$multiplexer = new MultiplexerClient([$mockClient1, $mockClient2, $mockClient3]);
161+
162+
$this->assertSame($multiplexer, $multiplexer->clear());
163+
}
164+
146165
public function provideInvalidClient()
147166
{
148167
return [

0 commit comments

Comments
 (0)