enable caching for comments
This commit is contained in:
@@ -4,5 +4,6 @@ $config = [
|
|||||||
'user-id' => 379833,
|
'user-id' => 379833,
|
||||||
'search-url' => 'https?://fsfe.org', // please use https?:// as schema
|
'search-url' => 'https?://fsfe.org', // please use https?:// as schema
|
||||||
'cache_toots' => 300,
|
'cache_toots' => 300,
|
||||||
|
'cache_comments' => 300,
|
||||||
'debug' => true
|
'debug' => true
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ $(document).ready(function() {
|
|||||||
url: "/comments/getcomments.php",
|
url: "/comments/getcomments.php",
|
||||||
type: "get",
|
type: "get",
|
||||||
data: {
|
data: {
|
||||||
search : "https://fsfe.org/news/2019/news-20190326-01.html"
|
search : RelPermalink
|
||||||
},
|
},
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
var stats = data.stats;
|
var stats = data.stats;
|
||||||
|
|||||||
@@ -7,8 +7,10 @@ $searchurl = $config['search-url'];
|
|||||||
$search = isset($_GET['search']) ? $_GET['search'] : '';
|
$search = isset($_GET['search']) ? $_GET['search'] : '';
|
||||||
$debug_on = $config['debug'];
|
$debug_on = $config['debug'];
|
||||||
/* cache files */
|
/* cache files */
|
||||||
$ctt = $config['cache_toots'];;
|
$ctt = $config['cache_toots'];
|
||||||
$dbt = "cache-toots.json";
|
$dbt = "cache-toots.json";
|
||||||
|
$ctc = $config['cache_comments'];
|
||||||
|
$dbc = "cache-comments_%id.json";
|
||||||
|
|
||||||
/* Exit if search empty */
|
/* Exit if search empty */
|
||||||
if (empty($search)) {
|
if (empty($search)) {
|
||||||
@@ -26,7 +28,11 @@ function debug($data) {
|
|||||||
|
|
||||||
/* CACHE FUNCTIONS */
|
/* CACHE FUNCTIONS */
|
||||||
/* write data to file */
|
/* 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['toots'] = $data;
|
||||||
$file['timestamp'] = time();
|
$file['timestamp'] = time();
|
||||||
// encode and write file
|
// encode and write file
|
||||||
@@ -34,21 +40,26 @@ function write_db($db, $data) {
|
|||||||
file_put_contents($db, $encoded, LOCK_EX);
|
file_put_contents($db, $encoded, LOCK_EX);
|
||||||
}
|
}
|
||||||
/* access data from file */
|
/* access data from file */
|
||||||
function read_db($db, &$data, &$cachebreak) {
|
function read_db($db, &$data, $cachetime, &$cachebreak, $id) {
|
||||||
global $ctt;
|
// 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 DB does not exist, create it with empty array
|
||||||
if (! file_exists($db)) {
|
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);
|
touch($db);
|
||||||
write_db($db, array());
|
write_db($db, $data, $id);
|
||||||
$cachebreak = true;
|
$cachebreak = true;
|
||||||
}
|
}
|
||||||
$file = file_get_contents($db, true);
|
$file = file_get_contents($db, true);
|
||||||
$data = json_decode($file, true);
|
$data = json_decode($file, true);
|
||||||
|
|
||||||
debug($data['timestamp']);
|
|
||||||
|
|
||||||
// check if timestamp in cache file too old
|
// 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;
|
$cachebreak = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +78,7 @@ function collectToots($instance, $uid, $min_id, $searchurl) {
|
|||||||
}
|
}
|
||||||
/* Find out if a toot contains the searched URL */
|
/* Find out if a toot contains the searched URL */
|
||||||
function analyzeToot($instance, $id, $searchurl) {
|
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");
|
$raw = file_get_contents("$instance/api/v1/statuses/$id");
|
||||||
$json = json_decode($raw, true);
|
$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 */
|
/* 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
|
// this at the same time loads the cached DB, either way
|
||||||
$cachebreak = false;
|
$cachebreak = false;
|
||||||
read_db($dbt, $toots, $cachebreak);
|
read_db($dbt, $toots, $ctt, $cachebreak, false);
|
||||||
|
|
||||||
if ($cachebreak) {
|
if ($cachebreak) {
|
||||||
/* Collect all the toots */
|
/* Collect all the toots */
|
||||||
/* get id of latest cached toot, and set as $min_id */
|
/* 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'])) {
|
if (!empty($toots['0']['id'])) {
|
||||||
$min_id_cached = $toots['0']['id'];
|
$min_id_cached = $toots['0']['id'];
|
||||||
$min_id = $min_id_cached;
|
$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
|
// min_id is the latest, let's write the new DB and end this loop
|
||||||
$uptodate = true;
|
$uptodate = true;
|
||||||
debug("Toots up-to-date. Rewrite cache DB.");
|
debug("Toots up-to-date. Rewrite cache DB.");
|
||||||
write_db($dbt, $toots);
|
write_db($dbt, $toots, false);
|
||||||
} else {
|
} else {
|
||||||
// next round looks for toots newer than the newly found ID
|
// next round looks for toots newer than the newly found ID
|
||||||
debug("Newer toots than in cache found. Starting another search for new toots");
|
debug("Newer toots than in cache found. Starting another search for new toots");
|
||||||
@@ -168,7 +179,7 @@ if ($cachebreak) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debug("Toots cache is OK");
|
debug("Toots cache is up-to-date");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* prepare $result array */
|
/* prepare $result array */
|
||||||
@@ -182,12 +193,23 @@ if (empty($id)) {
|
|||||||
// if multiple exist, take the oldest one (highest array position)
|
// if multiple exist, take the oldest one (highest array position)
|
||||||
$id = $toots[end($id)]['id'];
|
$id = $toots[end($id)]['id'];
|
||||||
|
|
||||||
/* Extract comments and stats from toot */
|
/* read cached comments, or reload new comments if cached data too old */
|
||||||
tootContext($instance, $id, $result);
|
$cachebreak = false;
|
||||||
tootStats($instance, $id, $result);
|
read_db($dbc, $result, $ctc, $cachebreak, $id);
|
||||||
// FIXME: At the moment the API doesn't return the correct replies count so I count it manually
|
|
||||||
$result['stats']['replies'] = count($result['comments']);
|
if ($cachebreak) {
|
||||||
$result['stats']['root'] = $id;
|
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
|
// headers for not caching the results
|
||||||
|
|||||||
Reference in New Issue
Block a user