Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
Fix caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Waite committed Mar 15, 2022
1 parent cb72fcf commit 7f03c3c
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,18 @@ protected function makeRequest($type, $options)
$request = $this->requestFactory->create($type, ['key' => $this->apiKey] + $options);
try {

$rawResponse = $this->makeHttpRequest($request);
$formattedResponse = $this->responseParser->parseResult($request, $rawResponse);
$response = $this->resultFactory->create($formattedResponse);
$this->trackApiUsage(ApiNames::SEMRUSH, $request->getEndpoint(), true, [
'usage' => $this->getApiUsage($request, $response)
]);
return $response;
$cacheKey = $this->urlBuilder->build($request);

return $this->cache(md5($cacheKey), function() use ($request) {
$rawResponse = $this->makeHttpRequest($request);
$formattedResponse = $this->responseParser->parseResult($request, $rawResponse);
$response = $this->resultFactory->create($formattedResponse);
$this->trackApiUsage(ApiNames::SEMRUSH, $request->getEndpoint(), true, [
'usage' => $this->getApiUsage($request, $response)
]);

return $response;
});
} catch (BadResponseException $e) {
$this->trackApiUsage(ApiNames::SEMRUSH, $request->getEndpoint(), false);
throw $this->parseBadResponse($e);
Expand Down Expand Up @@ -312,14 +317,12 @@ protected function makeHttpRequest(Request $request): string
{
$url = $this->urlBuilder->build($request);

return $this->cache(md5($url), function() use ($url) {
$guzzleResponse = $this->guzzle->request('GET', $url, [
RequestOptions::CONNECT_TIMEOUT => $this->connectTimeout,
RequestOptions::TIMEOUT => $this->timeout
]);
$guzzleResponse = $this->guzzle->request('GET', $url, [
RequestOptions::CONNECT_TIMEOUT => $this->connectTimeout,
RequestOptions::TIMEOUT => $this->timeout
]);

return $guzzleResponse->getBody()->getContents();
});
return $guzzleResponse->getBody()->getContents();
}

/**
Expand Down

0 comments on commit 7f03c3c

Please sign in to comment.