The official Phalcon docs show a simple example on how to cache a page:
//Create an Output frontend. Cache the files for 2 days
$frontCache = new Phalcon\Cache\Frontend\Output(array(
"lifetime" => 172800
));
// Set the cache file directory
$cache = new Phalcon\Cache\Backend\File($frontCache, array(
"cacheDir" => "../app/cache/"
));
// Get/Set the cache file to ../app/cache/my-cache.html
$content = $cache->start("my-cache.html");
// If $content is null then the content will be generated for the cache
if ($content === null) {
// Generate the page and store the output into the cache file
// [... your elements and tags here ... ]
$cache->save();
} else {
// Echo the cached output
echo $content;
}
The snippet is ok, but how can I include it inside a controller, so that I can cache the page i.e. the html generated by the view component? As a bonus, can I also avoid to specify the file name in $cache->start("my-cache.html");
and let Phalcon guess the right name automagically?