官方Phalcon docs显示了一个关于如何缓存页面的简单示例:
//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;
}
该片段没问题,但是如何将其包含在控制器中,以便我可以缓存页面,即视图组件生成的html?作为奖励,我还可以避免在$cache->start("my-cache.html");
中指定文件名并让Phalcon自动猜出正确的名称吗?
答案 0 :(得分:1)
Volt对视图片段的缓存包含在Phalcon Framework中。阅读这部分文档:
http://docs.phalconphp.com/en/latest/reference/volt.html#caching-view-fragments
文档中的一些示例:
{% cache ("article-" ~ post.id) 3600 %}
<h1>{{ post.title }}</h1>
<p>{{ post.content }}</p>
{% endcache %}
另一个:
{# cache the sidebar by 1 hour #}
{% cache "sidebar" 3600 %}
<!-- generate this content is slow so we are going to cache it -->
{% endcache %}