GNU/Weeb Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: Ammar Faizi <[email protected]>
To: GNU/Weeb FB Team <[email protected]>
Cc: Ammar Faizi <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>,
	Michael William Jonathan <[email protected]>
Subject: [PATCH fb v1 1/6] fb: Introduce `getCache()` and `setCache()` functions
Date: Tue,  9 May 2023 17:46:53 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

A preparation patch to implement better caching mechanism. All methods
that need cache will call these functions.

Signed-off-by: Ammar Faizi <[email protected]>
---
 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


  reply	other threads:[~2023-05-09 10:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-09 10:46 [PATCH fb v1 0/6] Introducing cache for the Facebook scraper Ammar Faizi
2023-05-09 10:46 ` Ammar Faizi [this message]
2023-05-09 10:46 ` [PATCH fb v1 2/6] fb: Post: Replace old cache mechanism in `getTimelineYears()` Ammar Faizi
2023-05-09 10:46 ` [PATCH fb v1 3/6] fb: Post: Implement cache in `getPost()` Ammar Faizi
2023-05-09 10:46 ` [PATCH fb v1 4/6] fb: Post: Implement cache in `getTimelinePosts()` Ammar Faizi
2023-05-09 10:46 ` [PATCH fb v1 5/6] fb: cache: Introduce `clearExpiredCaches()` Ammar Faizi
2023-05-09 10:46 ` [PATCH fb v1 6/6] fb: web: Create cron.php to clear cache Ammar Faizi
2023-05-09 11:06 ` [PATCH fb v1 0/6] Introducing cache for the Facebook scraper GNU/Weeb Facebook Team

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox