I wonder how why Phalcon does not have the cache method of caching whole action output. (or I dont know yet).
For example, I want to cache the entire home page of my site as a static html page, so that the server will display a html page instead of requesting to database many times.
I know that Phalcon has a cache method called Caching Output Fragments
<?php
//Create an Output frontend. Cache the files for 2 days
$frontCache = new Phalcon\Cache\Frontend\Output(array(
"lifetime" => 172800
));
// Create the component that will cache from the "Output" to a "File" backend
// Set the cache file directory - it's important to keep the "/" at the end of
// the value for the folder
$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) {
//Print date and time
echo date("r");
//Generate a link to the sign-up action
echo Phalcon\Tag::linkTo(
array(
"user/signup",
"Sign Up",
"class" => "signup-button"
)
);
// Store the output into the cache file
$cache->save();
} else {
// Echo the cached output
echo $content;
}
But it just implement the cache when there are some echo
calls in the action. If in the action, it just returns the result for the view - how to implement the Caching Output Fragments
above?
However, the needed cache is the final output of an action. It means that the render content of the view is exactly the date to cache. In the method above, it caches what the action echoes - not mention to the view.
Ex:
<?php
//Create an Output frontend. Cache the files for 2 days
$frontCache = new Phalcon\Cache\Frontend\Output(array(
"lifetime" => 172800
));
// Create the component that will cache from the "Output" to a "File" backend
// Set the cache file directory - it's important to keep the "/" at the end of
// the value for the folder
$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) {
//Print date and time
echo date("r");
//Generate a link to the sign-up action
echo Phalcon\Tag::linkTo(
array(
"user/signup",
"Sign Up",
"class" => "signup-button"
)
);
// Store the output into the cache file
$cache->save();
$results = Resultset::find();
$this->view->results = $results;
} else {
// Echo the cached output
echo $content;
}
I hope that we can found solution for this situation.