forked from glumb/pico_cache
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPicoZCache.php
74 lines (65 loc) · 2.46 KB
/
PicoZCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* Pico Cache plugin
* Name "PicoZCache" is to be loaded as last.
*
* @author Maximilian Beck before 2.0, Nepose since 2.0
* @link https://github.com/Nepose/PicoCache
* @license http://opensource.org/licenses/MIT
* @version 2.0
*/
class PicoZCache extends AbstractPicoPlugin
{
const API_VERSION=2;
protected $dependsOn = array();
private $cacheDir = 'content/cache/';
private $cacheTime = 604800; // 60*60*24*7, seven days
private $doCache = true;
private $cacheXHTML = false;
private $cacheFileName;
public function onConfigLoaded(array &$settings)
{
if (isset($config['cache_dir'])) {
// ensure cache_dir ends with '/'
$lastchar = substr($config['cache_dir'], -1);
if ($lastchar !== '/') {
$config['cache_dir'] = $config['cache_dir'].'/';
}
$this->cacheDir = $config['cache_dir'];
}
if (isset($config['cache_time'])) {
$this->cacheTime = $config['cache_time'];
}
if (isset($config['cache_enabled'])) {
$this->doCache = $config['cache_enabled'];
}
if (isset($config['cache_xhtml_output'])) {
$this->cacheXHTML = $config['cache_xhtml_output'];
}
}
public function onRequestUrl(&$url)
{
//replace any character except numbers and digits with a '-' to form valid file names
$this->cacheFileName = $this->cacheDir . preg_replace('/[^A-Za-z0-9_\-]/', '_', $url) . '.html';
//if a cached file exists and the cacheTime is not expired, load the file and exit
if ($this->doCache && file_exists($this->cacheFileName) && (time() - filemtime($this->cacheFileName)) < $this->cacheTime) {
header("Expires: " . gmdate("D, d M Y H:i:s", $this->cacheTime + filemtime($this->cacheFileName)) . " GMT");
($this->cacheXHTML) ? header('Content-Type: application/xhtml+xml') : header('Content-Type: text/html');
die(readfile($this->cacheFileName));
}
}
public function on404ContentLoaded(&$rawContent)
{
//don't cache error pages. This prevents filling up the cache with non existent pages
$this->doCache = false;
}
public function onPageRendered(&$output)
{
if ($this->doCache) {
if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir, 0755, true);
}
file_put_contents($this->cacheFileName, $output);
}
}
}