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=1683629238; bh=HRP+VcEmQMdXQeCvBoH41lNoRFp5v0OfrROx2TfNeE0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=GoVlKQIAo4JBRkEe09yk2RQvYjOEpbgGHoWz3UzeduhRSl2QIWhE203JEVSb+BiWP vTG7Qv27nyxHsjofDyoYFUrSUjoaiVxIhoy4/3TA4/cEECU185j0kwkaxZrQBKnqpM gDXkcIHO6Udkw2azwBH5B0hlZ30JXPLBSESfNjXPaiI9Iw9UKEERaL8giq2X6cPinw itvXy88ES8wSrpULUsdOkUKKAo+CNJc4dZP4xb385MqwhVFfUryzyxcHD+VjEhJmyD vdnbZ5pTulK+YuLYPPnHJX4qw6DZSKJUtoRHUSgJnTmVlkR1hrdKY0DPgASF6JQxWF 9WRe/IfdWfM3g== Received: from localhost.localdomain (unknown [128.199.192.202]) by gnuweeb.org (Postfix) with ESMTPSA id 7FD5D245C71; Tue, 9 May 2023 17:47:16 +0700 (WIB) From: Ammar Faizi To: GNU/Weeb FB Team Cc: Ammar Faizi , GNU/Weeb Mailing List , Michael William Jonathan Subject: [PATCH fb v1 5/6] fb: cache: Introduce `clearExpiredCaches()` Date: Tue, 9 May 2023 17:46:57 +0700 Message-Id: <20230509104658.70953-6-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: When a cache is expired, it won't be deleted unless getCache() with the corresponding key is invoked. Introduce a new function to scan for expired caches and delete them. Signed-off-by: Ammar Faizi --- src/Facebook/Facebook.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Facebook/Facebook.php b/src/Facebook/Facebook.php index 16970714ddce7bdc..00aed812693248a8 100644 --- a/src/Facebook/Facebook.php +++ b/src/Facebook/Facebook.php @@ -308,4 +308,34 @@ class Facebook return $data["data"]; } + + /** + * @return void + */ + public function clearExpiredCaches(): void + { + $scan = scandir($this->cache_dir); + foreach ($scan as $file) { + $file = "{$this->cache_dir}/{$file}"; + if (!is_file($file)) { + continue; + } + + $data = @file_get_contents($file); + if (!$data) { + unlink($file); + continue; + } + + $data = @json_decode($data, true); + if (!isset($data["exp"]) || !isset($data["data"])) { + unlink($file); + continue; + } + + if ($data["exp"] < time()) { + unlink($file); + } + } + } } -- Ammar Faizi