Created
December 10, 2018 15:29
-
-
Save fl3pp/a3533beb653145043a62666ca261ba17 to your computer and use it in GitHub Desktop.
Adds basic caching functionality to Pico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Creates a 'cache.php' file in the content directory, containing all pages. | |
The file is being kept for a day, page changes will be ignored as long the cache file exists. | |
You can simply delete the cache file in order to refresh the pages and contents | |
*/ | |
class PicoCaching extends AbstractPicoPlugin { | |
const API_VERSION = 2; | |
private $cachingFile; | |
private $cache; | |
private $createCache = true; | |
public function onConfigLoaded(&$config) { | |
$this->cachingFile = $config['content_dir'].'cache.php'; | |
} | |
public function onPagesLoading() { | |
if (!file_exists($this->cachingFile)) { | |
return; | |
} | |
$cache = unserialize(file_get_contents($this->cachingFile)); | |
if ((time() - intval($cache['date_created'])) > 86400) { | |
unlink($this->cachingFile); | |
return; | |
} | |
$this->createCache = false; | |
$this->cache = $cache['cache']; | |
} | |
public function onSinglePageLoading($id, &$skipFile) { | |
if ($this->createCache == false) { | |
$skipFile = true; | |
} | |
} | |
public function onPagesLoaded(&$pages) { | |
if ($this->createCache) { | |
$cache = array('date_created' => time(), 'cache' => $pages); | |
file_put_contents($this->cachingFile, serialize($cache)); | |
} else { | |
$pages = $this->cache; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment