Skip to content

Commit

Permalink
prevent request storm if cache is empty and the request failed
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed Feb 16, 2021
1 parent e258b97 commit ddb9fb2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Enable environment variables for setting username (thanks to @niektenhoopen)
- Adding rel noopener and noreferrer properties to links (thanks to @JayBox325)

### Changed
- Prevent request storm if no items are cached and the request failed.

### Fixed
- Fixed background color of icon
- Add trailing slash to all requests
Expand Down
11 changes: 9 additions & 2 deletions src/services/InstagramService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use craft\helpers\FileHelper;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;

class InstagramService extends Component
{
Expand Down Expand Up @@ -63,6 +61,15 @@ public function getFeed(string $accountOrTag = null): array
Craft::$app->getCache()->set('instagram_data_' . $hash, $cachedItems, 2592000);
Craft::$app->getCache()->set('instagram_update_error_' . $hash, true, 900);
}

if (empty($items) && empty($cachedItems)) {
// If the cache is empty (e.g. first request ever) and the request fails, we are stopping requests for 15 minutes.
Craft::debug('Cache is empty and no items could be fetched. Stopping requests for 15 minutes.', __METHOD__);
Craft::$app->getCache()->set('instagram_data_' . $hash, [], 2592000);
Craft::$app->getCache()->set('instagram_update_error_' . $hash, true, 900);

return [];
}
}

Craft::debug('Returning cached items.', __METHOD__);
Expand Down

0 comments on commit ddb9fb2

Please sign in to comment.