From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1683629231; bh=6AOdWOryw5s142jquqaYoYmUxj9v+eiJc8gS214jBCA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=DGY2/2yBnCc/8rHrO0+b007ZzcVV0ZGgxChy2dgmDwrAguKt6TOn3w2HpZTr4RVyn FIuyQ95UHIK7yQsz25Jo7gDXermlRr3VBo+sEfREAmKbVKV4TcMumiQeyHRZneRPml D0s7CldiBsQciftQxp8gV9IN3ltxEciRl5jHCBs2usc61A/7MaZhj0B6sPZ1IiFDOJ /dWpxuailjWnWPgjA5P8enllK/GkZT5TsyerP6ad92OFE2+JdpfUidCEOMwdf85zNi nKDv4+/ZLzy+4Mh3dRrFo+A8TEBwoiQsydSCG26iiFP6EQj7tUc74NQ6vZNP38jXU4 ppGONWSOpqpOA== Received: from localhost.localdomain (unknown [128.199.192.202]) by gnuweeb.org (Postfix) with ESMTPSA id 980CF245B96; Tue, 9 May 2023 17:47:09 +0700 (WIB) From: Ammar Faizi To: GNU/Weeb FB Team Cc: Ammar Faizi , GNU/Weeb Mailing List , Michael William Jonathan Subject: [PATCH fb v1 2/6] fb: Post: Replace old cache mechanism in `getTimelineYears()` Date: Tue, 9 May 2023 17:46:54 +0700 Message-Id: <20230509104658.70953-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230509104658.70953-1-ammarfaizi2@gnuweeb.org> References: <20230509104658.70953-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: `getTimelineYears()` always fetches the endpoint online, then it sets the cache based on the fetch result. On the other hand, the `getTimelinePosts()` method always tries to read the cache that `getTimelineYears()` sets. Now, simplify the caching mechanism and make the `getTimelineYears()` cache private to itself. This also means that the endpoint "action=getTimelineYears" will utilize the cache. Signed-off-by: Ammar Faizi --- src/Facebook/Facebook.php | 24 --------------- src/Facebook/Methods/Post.php | 58 +++++------------------------------ 2 files changed, 8 insertions(+), 74 deletions(-) diff --git a/src/Facebook/Facebook.php b/src/Facebook/Facebook.php index 6411c26709c24307..16970714ddce7bdc 100644 --- a/src/Facebook/Facebook.php +++ b/src/Facebook/Facebook.php @@ -115,30 +115,6 @@ class Facebook } } - /** - * @param string $username - * @return string - */ - public function getUserCacheDir(string $username): string - { - $ret = $this->cache_dir."/".$username; - if (!is_dir($ret)) { - if (!mkdir($ret, 0755, true)) { - throw new \Exception("Cannot create user cache directory: {$ret}"); - } - } - - if (!is_writable($ret)) { - throw new \Exception("User cache directory is not writable: {$ret}"); - } - - if (!is_readable($ret)) { - throw new \Exception("User cache directory is not readable: {$ret}"); - } - - return $ret; - } - /** * @param string $user_agent * @return void diff --git a/src/Facebook/Methods/Post.php b/src/Facebook/Methods/Post.php index 988739568dddb9cb..81017c9122e6c341 100644 --- a/src/Facebook/Methods/Post.php +++ b/src/Facebook/Methods/Post.php @@ -38,66 +38,28 @@ trait Post return $years; } - /** - * Cache timeline year links. - * - * @param string $username - * @param array $years - * @return void - */ - private function setCacheTimelineYears(string $username, array $years) - { - $years = json_encode($years, JSON_INTERNAL_FLAGS); - $dir = $this->getUserCacheDir($username); - file_put_contents("{$dir}/timeline_years.json", $years); - } - - /** - * @param string $username - * @return array|null - */ - private function getCacheTimelineYears(string $username): ?array - { - $dir = $this->getUserCacheDir($username); - $file = "{$dir}/timeline_years.json"; - - if (!file_exists($file)) { - return NULL; - } - - /* - * Max cache time: 10 minutes. - */ - if (time() - filemtime($file) > 600) { - unlink($file); - return NULL; - } - - $years = json_decode(file_get_contents($file), true); - if (!is_array($years)) { - return NULL; - } - - return $years; - } - /** * @param string $username * @return array */ public function getTimelineYears(string $username): array { + $cacheKey = __METHOD__.$username; $username = trim($username); if ($username === "") { throw new \Exception("Username cannot be empty!"); } + $years = $this->getCache($cacheKey); + if (is_array($years)) + return $years; + $username = urlencode($username); $o = $this->http("/profile.php?id={$username}", "GET"); try { $ret = $this->parseTimelineYears($o["out"]); if (count($ret) > 0) { - $this->setCacheTimelineYears($username, $ret); + $this->setCache($cacheKey, $ret); return $ret; } } catch (\Exception $e) { @@ -118,7 +80,7 @@ trait Post $ret = $this->parseTimelineYears($o); if (count($ret) > 0) { - $this->setCacheTimelineYears($username, $ret); + $this->setCache($cacheKey, $ret); } return $ret; @@ -134,11 +96,7 @@ trait Post */ public function getTimelinePosts(string $username, int $year = -1, bool $take_content = false, int $limit = -1): array { - $years = $this->getCacheTimelineYears($username); - if (!is_array($years)) { - $years = $this->getTimelineYears($username); - } - + $years = $this->getTimelineYears($username); if ($year === -1) { $year = max(array_keys($years)); } -- Ammar Faizi