public inbox for [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 2/6] fb: Post: Replace old cache mechanism in `getTimelineYears()`
Date: Tue,  9 May 2023 17:46:54 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

`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 <[email protected]>
---
 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


  parent 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 ` [PATCH fb v1 1/6] fb: Introduce `getCache()` and `setCache()` functions Ammar Faizi
2023-05-09 10:46 ` Ammar Faizi [this message]
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