Caching and performance

speedmax edited this page Aug 18, 2010 · 15 revisions

Performance and Caching

In most cases, h2o is a fast(may not be the fastest) templating system it caches your template as soon it is parsed.
Performance of your template largely depends on

  • Turn off autoescape filtering
  • The size and complexity of your template.
  • Number of your sub-template (inclusion and inheritance).
  • Number of variable context lookup.

Speedy tips – fragment cache is your friend


From version 0.4 all variable output will be escaped when autoescape is enabled, if you know what you are doing then you can disable it to get that extra bit of performance. (not recommended for beginners)

Template Cache

Caching a template can increase performance since it skips step of inefficient template parsing,
H2o caches the template objects(internal data structure of a template) and bundled
multiple caching backend includes File, APC and memcache.

Template cache is turn-on by default, template cache is mostly transparent to the user and it will update
when you edit your template.

Cached template IS still dynamic which means you can render cached template with different variable context or data set

Variable cache

A very nice side effect of “with” tag which offers a way to create in shortcut variable in template also provide in-template variable cache

Lets say you have a project object that associated with many tasks, a tasks belongs to a owner. and you want to get the owner’s first name and email in template right?

<div id="owner_info">
    <strong>{{ project.tasks.first.owner.full_name }}</strong>
     <small>{{ }}</small>

That is awful lot of work

  • for your finger
  • for your database to get that full name
  • for h2o to perform context lookup to resolve that variable name into the value you want.

Variable cache in action

{% with project.tasks.first.owner as owner %}
<div id="owner_info">
    <strong>{{ owner.full_name }}</strong>
     <small>{{ }}</small>

{% endwith %}

Cleaner and that expensive operation perform once only.

Fragments cache

Fragments cache (also called partial output cache) is a bundled h2o extension, it caches the rendered output of the tag body.
It provides significant performance improvement when you output a resource intensive part while the rest of the template remains dynamic.

You can load any h2o extension on the fly either in your PHP script or template.

    require 'h2o.php'; 

    h2o::load('cache');  // load caching extension
    $tpl = new H2o('index.html', array('cache' => 'apc')); // fragments cache use same cache driver for template cache.

Cache a expensive operation and auto expire in 30 seconds

{% load 'cache' %}

{% cache 30 }
{{ site.get_active_users }}
endcache %}

Note: As you can see, cache extension can be load in template or in your PHP script, view H2o extensions for details about h2o extensions.

Cache backends

File cache

By default h2o uses file cache to store template objects, change h2o option cache_dir to where you
want to store template cache (ie: /tmp).

    $template = new H2o('homepage.tpl', array(
        'cache' => 'file',
        'cache_dir' => '/tmp'

APC cache

APC is a opt-code cache PHP extension that also provides a robust object cache,
and the performance is generally 10-30% faster than file caching.

    $template = new h2o('homepage.tpl', array('cache' => 'apc'));


coming soon