From b904c0e18a31eafc4a7ad4c8a8b75976db2da7f9 Mon Sep 17 00:00:00 2001 From: Dag Date: Wed, 13 Dec 2023 22:04:30 +0100 Subject: [PATCH] fix(reddit): tweak internal cache logic --- bridges/RedditBridge.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/bridges/RedditBridge.php b/bridges/RedditBridge.php index c393c146b19..2b7fe84f9a5 100644 --- a/bridges/RedditBridge.php +++ b/bridges/RedditBridge.php @@ -75,20 +75,26 @@ class RedditBridge extends BridgeAbstract public function collectData() { - $cacheKey = 'reddit_rate_limit'; - if ($this->cache->get($cacheKey)) { + $forbiddenKey = 'reddit_forbidden'; + if ($this->cache->get($forbiddenKey)) { + throw new HttpException('403 Forbidden', 403); + } + + $rateLimitKey = 'reddit_rate_limit'; + if ($this->cache->get($rateLimitKey)) { throw new HttpException('429 Too Many Requests', 429); } + try { $this->collectDataInternal(); } catch (HttpException $e) { - if ($e->getCode() === 429) { - $this->cache->set($cacheKey, true, 60 * 16); - } if ($e->getCode() === 403) { // 403 Forbidden // This can possibly mean that reddit has permanently blocked this server's ip address - $this->cache->set($cacheKey, true, 60 * 61); + $this->cache->set($forbiddenKey, true, 60 * 61); + } + if ($e->getCode() === 429) { + $this->cache->set($rateLimitKey, true, 60 * 16); } throw $e; }