2

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?

Ignorant
  • 2,411
  • 4
  • 31
  • 48
  • 1
    You can render view to the variable and then put it to the start() method. http://stackoverflow.com/questions/13901805/it-is-possible-to-get-phalcon-mvc-view-rendered-output-in-variable – Phantom Jun 29 '14 at 05:27
  • @Phantom pointing you in the right direction. I just thought to say that with Volt and other engines you get a good level of caching already. Caching the view output feels like an overkill? No? – Ian Bytchek Oct 06 '14 at 20:17

1 Answers1

1

Volt's caching of view fragments is included with Phalcon Framework. Read this part of docs:

http://docs.phalconphp.com/en/latest/reference/volt.html#caching-view-fragments

Some examples from docs:

{% cache ("article-" ~ post.id) 3600 %}

    <h1>{{ post.title }}</h1>

    <p>{{ post.content }}</p>

{% endcache %}

another one:

{# cache the sidebar by 1 hour #}
{% cache "sidebar" 3600 %}
    <!-- generate this content is slow so we are going to cache it -->
{% endcache %}
Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109