官方的Phalcon文档展示了一个如何缓存页面的简单示例:
//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自动猜测正确的名称吗?
Volt的视图片段缓存包含在Phalcon框架中。阅读文档的这一部分:
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 %}