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=1683629229; bh=vszRqdBBVa8PqaQlKbCgx/qDeTspzU67lW67C2TVDEE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Xu65GhWt5CWWyCVSoi3LSq8ysyFkjoHqz1il8B7xC6rCKMaccAEdHlUdE/3sijkWx Q/CxbF7M1zQLElD1NaXvSaUJOE6OKuXBs2T3VeGpv1KrtKuJS8uEhbHyzgDa/ku5nF p+2NUeb2taZLz4MXa5CFTPEN5u8CkrmTCYtRZHuc+uO2qEKLyx6Mn0oqIG/1WXfxr/ oz0ws+ZbuRf//hXxSmwiHY039nofzKeiJlCxfVcDiH8yyWbsgFmg7apSPSmjQ52639 QonhSg3iendSiwRE1Tch2n7jJ3ZMjJ9ZQXlbDmE6r4FjG66ZWBZBlOOEu+petgp8g7 DXO2whMvVxwJg== Received: from localhost.localdomain (unknown [128.199.192.202]) by gnuweeb.org (Postfix) with ESMTPSA id 3E8C8245C6C; Tue, 9 May 2023 17:47:06 +0700 (WIB) From: Ammar Faizi To: GNU/Weeb FB Team Cc: Ammar Faizi , GNU/Weeb Mailing List , Michael William Jonathan Subject: [PATCH fb v1 1/6] fb: Introduce `getCache()` and `setCache()` functions Date: Tue, 9 May 2023 17:46:53 +0700 Message-Id: <20230509104658.70953-2-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: A preparation patch to implement better caching mechanism. All methods that need cache will call these functions. Signed-off-by: Ammar Faizi --- src/Facebook/Facebook.php | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/Facebook/Facebook.php b/src/Facebook/Facebook.php index 2fe33f3b7cb6e9ff..6411c26709c24307 100644 --- a/src/Facebook/Facebook.php +++ b/src/Facebook/Facebook.php @@ -287,4 +287,49 @@ class Facebook return $url; } + + /** + * @param string $key + * @param mixed $data + * @param int $expire + * @return void + */ + private function setCache(string $key, $data, int $expire = 600): void + { + $key = str_replace(["/", "\\"], "_", $key); + $data = [ + "exp" => time() + $expire, + "data" => $data + ]; + $data = json_encode($data, JSON_INTERNAL_FLAGS); + if (!is_dir($this->cache_dir)) { + mkdir($this->cache_dir, 0777, true); + if (!is_dir($this->cache_dir)) { + throw new \Exception("Unable to create cache directory: {$this->cache_dir}"); + } + } + file_put_contents("{$this->cache_dir}/{$key}.json", $data); + } + + /** + * @param string $key + * @return mixed + */ + private function getCache(string $key) + { + $key = str_replace(["/", "\\"], "_", $key); + $file = "{$this->cache_dir}/{$key}.json"; + + if (!file_exists($file)) { + return NULL; + } + + $data = json_decode(file_get_contents($file), true); + if (!isset($data["exp"]) || !isset($data["data"])) { + unlink($file); + return NULL; + } + + return $data["data"]; + } } -- Ammar Faizi