@@ -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