dev-master
9999999-dev https://github.com/dan1elhughes/cacherSimple file caching for PHP with string replacements
by Dan Hughes
Wallogit.com
2017 © Pedro Peláez
Simple file caching for PHP with string replacements
Cacher provides a simple caching functionality, usable with two lines of code., (*1)
Add "xes/cacher": "dev-master" to your composer.json file., (*2)
Before any HTML is output:, (*3)
<?php
$cache = new xes\Cacher('/path/to/cache/folder');
$cache->start();
?>
The path must already exist and be writable by the web server., (*4)
At the end of your script:, (*5)
<?php $cache->finish(); ?>
For content such as page title and meta descriptions, you may not know the values until later in the script. For this there is a replacement method in Cacher:, (*6)
<title></title>
setReplacements( array(
"" => $pageTitleFromDatabase,
"" => $somethingElse
));
?>
This simply performs a find-and-replace on the HTML until the search text doesn't exist., (*7)
The replacements array can also be passed directly to finish()., (*8)
setEnabled(true/false) - choose if the cache should run or not. Useful for development environments., (*9)
setFolder(path) - absolute path to the cache folder. Must be writable by web server., (*10)
setTimeout(numberOfSeconds) - how many seconds should elapse until a cached file should be considered invalid., (*11)
setSuffix(fileSuffix) - the file suffix appended to all cache files. .cache.html by default., (*12)
setReplacements(array) - associative array of replacements in the format "find" -> "replace", (*13)
Simple file caching for PHP with string replacements