Skip to content

Commit fb5d723

Browse files
feat: Prevent in-memory storing when "Cache-Control: no-store" is used as request header
Precondition 1: There are two different caches: The request cache and the response cache. The respones cache can utilize the response data, which allows for tagging cache entries and hence fine grained retention calculation, so the request cache can be used across different PHP processes and potentially across different users. The request cache can only utlize what's known before the request was made. We usually use a transient memory cache for requests. This allows a single PHP process to issue the same GET request multiple times without needing to flush anything, because the cache content gets dropped as soon as the PHP process is over. Precondition 2: Requests not only transport their URI but an array of request heraders as well. Those request headers are meant to pass to the upstream server. Usually, request headers are part of the response cache header, so different request header values might cause cache misses e.g. for basic auth. Feature: The request mechanism now prevents the "request cache" (as of the second section above) to be used at all.
1 parent 5e4cd5e commit fb5d723

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

Classes/Service/ConsumerBackend.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Psr\Http\Message\ResponseInterface;
2828
use Psr\Http\Message\UriInterface;
2929

30+
use function strtolower;
31+
3032
/**
3133
* @Flow\Scope("singleton")
3234
*/
@@ -256,10 +258,18 @@ protected function requestJson(Uri $uri): PromiseInterface
256258
$uriString = (string)$uri;
257259

258260
$headers = $this->getRequestHeaders($uriString);
261+
259262
$headersForCacheIdentifier = [];
263+
$storeResponse = true;
264+
260265
foreach ($headers as $key => $value) {
266+
$key = strtolower($key);
267+
if ($key === 'cache-control' && strtolower($value) === 'no-store') {
268+
$storeResponse = false;
269+
}
261270
$headersForCacheIdentifier[] = sprintf('%s: %s', $key, $value);
262271
}
272+
263273
$cacheIdentifier = md5(serialize($headersForCacheIdentifier) . '|' . $uriString);
264274

265275
if ($this->requestsCache->has($cacheIdentifier)) {
@@ -269,8 +279,10 @@ protected function requestJson(Uri $uri): PromiseInterface
269279
} else {
270280
$response = $this
271281
->fetch($uri, $headers)
272-
->then(function(string $result) use ($cacheIdentifier) {
273-
$this->requestsCache->set($cacheIdentifier, $result);
282+
->then(function(string $result) use ($cacheIdentifier, $storeResponse) {
283+
if (!$storeResponse) {
284+
$this->requestsCache->set($cacheIdentifier, $result);
285+
}
274286
return $result;
275287
});
276288
}

0 commit comments

Comments
 (0)