Skip to content

Commit 9a2c500

Browse files
TerjeBrNyholm
authored andcommitted
Add body content to cache key when it is a POST request (#12)
* Add body content to cache key when it is a POST request * Change == to === * We need to search for and replace the apiKey in the body part of the cacheKey as well
1 parent 75e6dc9 commit 9a2c500

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/CachedResponseClient.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ public function __construct(HttpClient $delegate, $cacheDir, $apiKey = null)
5454
*/
5555
public function sendRequest(RequestInterface $request)
5656
{
57-
$url = (string) $request->getUri();
5857
$host = (string) $request->getUri()->getHost();
58+
$cacheKey = (string) $request->getUri();
59+
if ('POST' === $request->getMethod()) {
60+
$cacheKey .= $request->getBody();
61+
}
5962
if (!empty($this->apiKey)) {
60-
$url = str_replace($this->apiKey, '[apikey]', $url);
63+
$cacheKey = str_replace($this->apiKey, '[apikey]', $cacheKey);
6164
}
6265

63-
$file = sprintf('%s/%s_%s', $this->cacheDir, $host, sha1($url));
66+
$file = sprintf('%s/%s_%s', $this->cacheDir, $host, sha1($cacheKey));
6467
if (is_file($file) && is_readable($file)) {
6568
return new Response(200, [], (new StreamFactory())->createStream(unserialize(file_get_contents($file))));
6669
}

0 commit comments

Comments
 (0)