Skip to content

Commit 3d63aaf

Browse files
authored
Fix greedy cache deletion (#209)
* Add deletion tests for GreedyCacheStrategy * Fix GreedyCacheStrategy delete() cache key to use varyHeaders
1 parent 9e6f5eb commit 3d63aaf

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/Strategy/GreedyCacheStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ public function fetch(RequestInterface $request)
117117
*/
118118
public function delete(RequestInterface $request)
119119
{
120-
return $this->storage->delete($this->getCacheKey($request));
120+
return $this->storage->delete($this->getCacheKey($request, $this->varyHeaders));
121121
}
122122
}

tests/Strategy/GreedyCacheStrategyTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use GuzzleHttp\Psr7\Request;
99
use GuzzleHttp\Psr7\Response;
1010
use Kevinrob\GuzzleCache\CacheMiddleware;
11+
use Kevinrob\GuzzleCache\KeyValueHttpHeader;
1112
use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy;
1213
use Kevinrob\GuzzleCache\Storage\VolatileRuntimeStorage;
1314
use Psr\Http\Message\RequestInterface;
@@ -73,4 +74,51 @@ public function testHeaderTtl()
7374
$response = $this->client->send($request);
7475
$this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO));
7576
}
77+
78+
public function testGreedyCacheStrategyDeletion()
79+
{
80+
$storage = new VolatileRuntimeStorage();
81+
$strategy = new GreedyCacheStrategy($storage, 60);
82+
83+
$request = new Request('GET', 'http://test.com/');
84+
$response = new Response(200, [], 'Test content');
85+
86+
$result = $strategy->cache($request, $response);
87+
$this->assertTrue($result, 'Response should be cached');
88+
89+
$cached = $strategy->fetch($request);
90+
$this->assertNotNull($cached, 'Cache entry should exist');
91+
$this->assertEquals('Test content', (string) $cached->getResponse()->getBody());
92+
93+
$deleted = $strategy->delete($request);
94+
$this->assertTrue($deleted, 'Cache entry should be deleted successfully');
95+
96+
$cached = $strategy->fetch($request);
97+
$this->assertNull($cached, 'Cache entry should no longer exist after deletion');
98+
}
99+
100+
public function testGreedyDeletionWithVaryHeaders()
101+
{
102+
$storage = new VolatileRuntimeStorage();
103+
$varyHeaders = new KeyValueHttpHeader(['Authorization']);
104+
$strategy = new GreedyCacheStrategy($storage, 60, $varyHeaders);
105+
106+
$request = new Request('GET', 'http://test.com/', [
107+
'Authorization' => 'Bearer token123'
108+
]);
109+
$response = new Response(200, [], 'Authorized content');
110+
111+
$result = $strategy->cache($request, $response);
112+
$this->assertTrue($result, 'Response should be cached with vary headers');
113+
114+
$cached = $strategy->fetch($request);
115+
$this->assertNotNull($cached, 'Cache entry should exist with vary headers');
116+
$this->assertEquals('Authorized content', (string) $cached->getResponse()->getBody());
117+
118+
$deleted = $strategy->delete($request);
119+
$this->assertTrue($deleted, 'Cache entry should be deleted successfully with vary headers');
120+
121+
$cached = $strategy->fetch($request);
122+
$this->assertNull($cached, 'Cache entry should no longer exist after deletion with vary headers');
123+
}
76124
}

0 commit comments

Comments
 (0)