Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions PicoZCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,19 @@ class PicoZCache extends AbstractPicoPlugin
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 onConfigLoaded(array &$config)
{
$this->doCache = $this->getPluginConfig('enabled', false);
$this->cacheTime = $this->getPluginConfig('time', 604800);
$this->cacheXHTML = $this->getPluginConfig('xhtml_output', false);
$this->cacheDir = trim($this->getPluginConfig('dir', 'content/cache/'),'/').'/';
}

public function onRequestUrl(&$url)
{
$query = (!empty($_GET)) ? '__'.md5(serialize($_GET)) : null;
//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';
$this->cacheFileName = $this->cacheDir . preg_replace('/[^A-Za-z0-9_\-]/', '_', $url.$query) . '.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) {
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ To install the Pico Cache plugin, simply download the `PicoZCache.php` and put i

In config.yml you can change default caching settings:
```yaml
cache_enabled: true # True/False if cache is enabled
cache_dir: content/cache/ # Directory where cache should be saved
cache_time: 604800 # Interval between caching (period from one to second cache) in seconds, here is 7 days = 60 * 60 * 24 * 7.
cache_xhtml_output: false # If true, XHTML Content-Type header will be sent when loading cache page
PicoZCache:
enabled: true # True/False if cache is enabled
dir: cache/html/ # Directory where cache should be saved
time: 604800 # Interval between caching (period from one to second cache) in seconds, here is 7 days = 60 * 60 * 24 * 7.
xhtml_output: false # If true, XHTML Content-Type header will be sent when loading cache page
```

## Cache clearing
Expand Down