From dcd79bb3430d85f739179fb01e34a7708fbab396 Mon Sep 17 00:00:00 2001 From: "max.mehl" Date: Thu, 17 Oct 2019 01:04:44 +0200 Subject: [PATCH] enable caching for comments --- .../static/comments/config.sample.php | 1 + .../static/comments/getcomments.js | 2 +- .../static/comments/getcomments.php | 60 +++++++++++++------ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/themes/hugo-mastodon-comments/static/comments/config.sample.php b/themes/hugo-mastodon-comments/static/comments/config.sample.php index daec92d..bbfa309 100644 --- a/themes/hugo-mastodon-comments/static/comments/config.sample.php +++ b/themes/hugo-mastodon-comments/static/comments/config.sample.php @@ -4,5 +4,6 @@ $config = [ 'user-id' => 379833, 'search-url' => 'https?://fsfe.org', // please use https?:// as schema 'cache_toots' => 300, + 'cache_comments' => 300, 'debug' => true ]; diff --git a/themes/hugo-mastodon-comments/static/comments/getcomments.js b/themes/hugo-mastodon-comments/static/comments/getcomments.js index 9b31851..3d47f86 100644 --- a/themes/hugo-mastodon-comments/static/comments/getcomments.js +++ b/themes/hugo-mastodon-comments/static/comments/getcomments.js @@ -13,7 +13,7 @@ $(document).ready(function() { url: "/comments/getcomments.php", type: "get", data: { - search : "https://fsfe.org/news/2019/news-20190326-01.html" + search : RelPermalink }, success: function(data) { var stats = data.stats; diff --git a/themes/hugo-mastodon-comments/static/comments/getcomments.php b/themes/hugo-mastodon-comments/static/comments/getcomments.php index f1486d8..fe418b4 100644 --- a/themes/hugo-mastodon-comments/static/comments/getcomments.php +++ b/themes/hugo-mastodon-comments/static/comments/getcomments.php @@ -7,8 +7,10 @@ $searchurl = $config['search-url']; $search = isset($_GET['search']) ? $_GET['search'] : ''; $debug_on = $config['debug']; /* cache files */ -$ctt = $config['cache_toots'];; +$ctt = $config['cache_toots']; $dbt = "cache-toots.json"; +$ctc = $config['cache_comments']; +$dbc = "cache-comments_%id.json"; /* Exit if search empty */ if (empty($search)) { @@ -26,7 +28,11 @@ function debug($data) { /* CACHE FUNCTIONS */ /* write data to file */ -function write_db($db, $data) { +function write_db($db, $data, $id) { + // if $id is given, it's a comments file. Replace placeholder in filename + if ($id) { + $db = str_replace('%id', $id, $db); + } $file['toots'] = $data; $file['timestamp'] = time(); // encode and write file @@ -34,21 +40,26 @@ function write_db($db, $data) { file_put_contents($db, $encoded, LOCK_EX); } /* access data from file */ -function read_db($db, &$data, &$cachebreak) { - global $ctt; +function read_db($db, &$data, $cachetime, &$cachebreak, $id) { + // if $id is given, it's a comments file. Replace placeholder in filename + if ($id) { + $db = str_replace('%id', $id, $db); + } // if DB does not exist, create it with empty array if (! file_exists($db)) { + // if $data empty (usually with $toots, not with comment's $result), populate with empty array + if (empty($data)) { + $data = array(); + } touch($db); - write_db($db, array()); + write_db($db, $data, $id); $cachebreak = true; } $file = file_get_contents($db, true); $data = json_decode($file, true); - debug($data['timestamp']); - // check if timestamp in cache file too old - if (empty($data['timestamp']) || ($data['timestamp'] + $ctt < time())) { + if (empty($data['timestamp']) || ($data['timestamp'] + $cachetime < time())) { $cachebreak = true; } @@ -67,7 +78,7 @@ function collectToots($instance, $uid, $min_id, $searchurl) { } /* Find out if a toot contains the searched URL */ function analyzeToot($instance, $id, $searchurl) { - debug("Searching for $searchurl in $id"); + //debug("Searching for $searchurl in $id"); $raw = file_get_contents("$instance/api/v1/statuses/$id"); $json = json_decode($raw, true); @@ -134,12 +145,12 @@ function tootStats($instance, $id, &$result) { /* check whether the cached file containing all toots is older than max. cache time */ // this at the same time loads the cached DB, either way $cachebreak = false; -read_db($dbt, $toots, $cachebreak); +read_db($dbt, $toots, $ctt, $cachebreak, false); if ($cachebreak) { /* Collect all the toots */ /* get id of latest cached toot, and set as $min_id */ - debug("Toots cache oudated. Checking for new toots"); + debug("Toots cache outdated. Checking for new toots"); if (!empty($toots['0']['id'])) { $min_id_cached = $toots['0']['id']; $min_id = $min_id_cached; @@ -160,7 +171,7 @@ if ($cachebreak) { // min_id is the latest, let's write the new DB and end this loop $uptodate = true; debug("Toots up-to-date. Rewrite cache DB."); - write_db($dbt, $toots); + write_db($dbt, $toots, false); } else { // next round looks for toots newer than the newly found ID debug("Newer toots than in cache found. Starting another search for new toots"); @@ -168,7 +179,7 @@ if ($cachebreak) { } } } else { - debug("Toots cache is OK"); + debug("Toots cache is up-to-date"); } /* prepare $result array */ @@ -182,12 +193,23 @@ if (empty($id)) { // if multiple exist, take the oldest one (highest array position) $id = $toots[end($id)]['id']; - /* Extract comments and stats from toot */ - tootContext($instance, $id, $result); - tootStats($instance, $id, $result); - // FIXME: At the moment the API doesn't return the correct replies count so I count it manually - $result['stats']['replies'] = count($result['comments']); - $result['stats']['root'] = $id; + /* read cached comments, or reload new comments if cached data too old */ + $cachebreak = false; + read_db($dbc, $result, $ctc, $cachebreak, $id); + + if ($cachebreak) { + debug("Comments cache for $id outdated. Checking for new comments"); + /* Extract comments and stats from toot */ + tootContext($instance, $id, $result); + tootStats($instance, $id, $result); + // FIXME: At the moment the API doesn't return the correct replies count so I count it manually + $result['stats']['replies'] = count($result['comments']); + $result['stats']['root'] = $id; + + write_db($dbc, $result, $id); + } else { + debug("Comments cache for $id up-to-date. Returning cached comments"); + } } // headers for not caching the results