Skip to content

Commit 8303f37

Browse files
committed
Computed proper cache keys
1 parent a7095cf commit 8303f37

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

mago.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ excessive-parameter-list = {
5050

5151
# Our API client has many methods.
5252
too-many-methods = {
53-
threshold = 32
53+
threshold = 33
5454
}
5555

5656
# Our API client is not really that complex …
5757
cyclomatic-complexity = {
58-
threshold = 45
58+
threshold = 47
5959
}
6060

6161
too-many-properties = {
6262
threshold = 11
6363
}
6464

6565
kan-defect = {
66-
threshold = 2.41
66+
threshold = 2.48
6767
}
6868

6969
[analyzer]

src/ApiClient.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function __construct(array $options)
9292
public function getServiceIndex(): array
9393
{
9494
$cache = $this->getCache();
95-
$cacheKey = sha1(__METHOD__);
95+
$cacheKey = $this->computeCacheKey(['method' => __METHOD__]);
9696

9797
// @mago-ignore analysis:less-specific-return-statement
9898
return $cache->get($cacheKey, function (CacheItemInterface $item): array {
@@ -412,7 +412,7 @@ public function getDocumentContent(Document $document): ResponseInterface
412412
protected function getAccessToken(): array
413413
{
414414
$cache = $this->getCache();
415-
$cacheKey = sha1(__METHOD__);
415+
$cacheKey = $this->computeCacheKey(['method' => __METHOD__]);
416416

417417
return $cache->get($cacheKey, function (CacheItemInterface $item): array {
418418
$client = $this->client();
@@ -536,7 +536,7 @@ protected function getSearchRequestUrl(string $rel, array $values): string
536536

537537
try {
538538
$cache = $this->getCache();
539-
$cacheKey = sha1(__METHOD__.'|||'.$rel);
539+
$cacheKey = $this->computeCacheKey(['method' => __METHOD__, 'rel' => $rel]);
540540

541541
$url = $cache->get($cacheKey, function (CacheItemInterface $item) use ($url) {
542542
$item->expiresAfter((int) $this->options['cache_item_lifetime']);
@@ -586,6 +586,28 @@ protected function getCache(): CacheInterface
586586
return new ProxyAdapter(pool: $pool);
587587
}
588588

589+
/**
590+
* Compute a cache key based on the client configuration and a context.
591+
*
592+
* @param array<string, string> $context
593+
*/
594+
protected function computeCacheKey(array $context): string
595+
{
596+
if (0 === count($context)) {
597+
throw new RuntimeException('Cache key context cannot be empty');
598+
}
599+
try {
600+
return sha1(json_encode($this->options + [$context], JSON_THROW_ON_ERROR));
601+
} catch (\Exception $exception) {
602+
// JSON encode context without throwing any exceptions.
603+
$encodedContext = (string) json_encode($context);
604+
throw new RuntimeException(
605+
sprintf('Cannot compute cache key for context %s', $encodedContext),
606+
previous: $exception,
607+
);
608+
}
609+
}
610+
589611
/**
590612
* @return Atom[]
591613
*

0 commit comments

Comments
 (0)