Skip to content

Commit

Permalink
cache time from file
Browse files Browse the repository at this point in the history
  • Loading branch information
attogram committed Apr 22, 2020
1 parent 3a4f26d commit 5a7f495
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

class Base
{
const VERSION = '0.6.2';
const VERSION = '0.7.0';

/** Cache Time, in seconds. 4 days = 345600 seconds */
const CACHE_TIME = 345600;
/** Cache Time, in seconds. 4 days = 345600, 14 days = 1209600 */
const CACHE_TIME = 1209600;

const DATE_FORMAT = 'Y-m-d H:i:s';

/** key strings */
const ABOUT = 'about';
Expand Down
13 changes: 8 additions & 5 deletions src/Mediawiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public function links($query)

return $result; // 404 Not Found
}
// set title
$result[self::TITLE] = $data[self::PARSE][self::TITLE];


$result['cached'] = time(); // set cache time
$result[self::TITLE] = $data[self::PARSE][self::TITLE]; // set title
// set reference links
$result[self::REFS] = isset($data[self::PARSE][self::EXTERNALLINKS])
? $data[self::PARSE][self::EXTERNALLINKS]
Expand Down Expand Up @@ -93,15 +95,16 @@ public function search($query)

return false;
}
$results = [];
$result = [];
$result['cached'] = time(); // set cache time
foreach ($data[self::QUERY][self::SEARCH] as $topic) {
if (!isset($topic[self::TITLE]) || !is_string($topic[self::TITLE])) {
continue;
}
$results[] = $topic[self::TITLE];
$result[] = $topic[self::TITLE];
}

return $results;
return $result;
}

/**
Expand Down
16 changes: 9 additions & 7 deletions src/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function get()
$this->initFilesystem();

if ($this->setDataFromCache()) { // get topic data from Cache
if (!empty($this->data['error'])) {
if (!empty($this->data['error'])) { // if cache contains error report (404)
$this->error404(self::ERROR_NOT_FOUND, $this->topic);

return;
Expand Down Expand Up @@ -108,13 +108,15 @@ private function display()

// set Extraction source url
$this->template->set('source', $this->source . $this->encodeLink($this->data[self::TITLE]));

// set Data and Cache age
$filesystemCache = new FilesystemCache();
$this->template->set('dataAge', $filesystemCache->getAge($this->data[self::TITLE]));
$this->template->set('now', gmdate('Y-m-d H:i:s'));
$this->template->set('now', gmdate(self::DATE_FORMAT));

if (!isset($this->data['cached'])) { // if no cache time is set...
$this->data['cached'] = time();
}
$this->template->set('cached', gmdate(self::DATE_FORMAT, $this->data['cached']));

$this->template->set(
'refresh',
'refresh', // refresh link
$this->template->get('home') . 'refresh/' . $this->encodeLink($this->data[self::TITLE])
);
$this->template->set('h1', $this->data[self::TITLE]);
Expand Down
2 changes: 1 addition & 1 deletion templates/topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<a href="#draft"><b><?= $this->get('draft_count') ?></b> Drafts</a>,
<a href="#user"><b><?= $this->get('user_count') ?></b> Users</a>
</li>
<li>Cached <?= $this->get('dataAge') ?> UTC (<a href="<?= $this->get('refresh') ?>">refresh</a>)</li>
<li>Cached <?= $this->get('cached') ?> UTC (<a href="<?= $this->get('refresh') ?>">refresh</a>)</li>
<li>Served <?= $this->get('now') ?> UTC</li>
<li>Extracted from &lt;<a href="<?= $this->get('source') ?>" target="_blank"><?= $this->get('source') ?></a>&gt;
released under the Creative Commons Attribution-Share-Alike License 3.0</li>
Expand Down

0 comments on commit 5a7f495

Please sign in to comment.