restructure cache file to contain timestamp

This commit is contained in:
2019-10-16 23:43:56 +02:00
parent 6993cdcee9
commit a5924efd91

View File

@@ -28,8 +28,10 @@ function debug($data) {
/* CACHE FUNCTIONS */
/* write data to file */
function write_db($db, $data) {
$file['toots'] = $data;
$file['timestamp'] = time();
// encode and write file
$encoded = json_encode($data, JSON_PRETTY_PRINT);
$encoded = json_encode($file, JSON_PRETTY_PRINT);
file_put_contents($db, $encoded, LOCK_EX);
}
/* access data from file */
@@ -40,7 +42,7 @@ function read_db($db, &$data) {
write_db($db, array());
}
$file = file_get_contents($db, true);
$data = json_decode($file, true);
$data = json_decode($file, true)['toots'];
}
/* TOOT FUNCTIONS */
@@ -142,7 +144,7 @@ while ($uptodate === false) {
if ($min_id_new === $min_id) {
// min_id is the latest, let's write the new DB and end this loop
$uptodate = true;
debug("Rewrite cache DB.");
debug("Toots up-to-date. Rewrite cache DB.");
write_db($dbt, $toots);
} else {
// next round looks for toots newer than the newly found ID
@@ -158,7 +160,6 @@ $result = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies'
$id = array_keys(array_column($toots, 'url'), strtolower($search));
if (empty($id)) {
debug("Blog URL \"$search\" has not been found");
die();
} else {
// if multiple exist, take the oldest one (highest array position)
$id = $toots[end($id)]['id'];