2017 © Pedro Peláez

craft-plugin presto

A static cache extension for Craft chaching



A static cache extension for Craft chaching

  • Tuesday, July 24, 2018
  • by lewiscommunications
  • Repository
  • 5 Watchers
  • 14 Stars
  • 30 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 131 % Grown


Presto, (*1)

Presto is a static file extension for the native Craft cache. It works alongside standard Twig {% cache %} tag pairs and includes cache-busting features. Just like standard caching, Presto is automatic. Simply install, update your layouts, and then the cache will bust automatically as you create, update, or delete content., (*2)


This plugin requires Craft CMS 3.0.0 or later., (*3)


To install the plugin, follow these instructions., (*4)

  1. Open your terminal and go to your Craft project:, (*5)

    cd /path/to/project
  2. Then tell Composer to load the plugin:, (*6)

    composer require lewiscom/presto
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Presto., (*7)

Setup Guide

Step 1 - Turn off element query caching

Turn off element query caching in your general config file. This will stop the DeleteStaleTemplateCaches task from running in the admin. Since Presto busts the entire cache when a new element is saved, element query caching is not necessary., (*8)

'cacheElementQueries' => false

Step 2 - Add cache tags

Presto lets Craft do the heavy lifting of calculating the elements within the template. As a result, all you need to do in your templates is pass the cache key returned from craft.presto.cache to the native cache tag pair. Presto will return a cache key that includes the host, group (if one is set), and path., (*9)

Note that the entirety of your template logic must be wrapped by the cache tags. In addition, it is recommended that you add the globally tag so that Craft does not overload the cache (i.e. query string requests)., (*10)

{% cache globally using key craft.presto.cache %}
    {# Template Logic #}
{% endcache %}


{% craft.presto.cache({
    group: 'pjax',
    static: false
}) %}
Parameter Type Description
group string When set, the requested page will write into a sub-folder within the top-level cache directory. This is useful for pjax implementations where you load a separate template.
static boolean Setting to false will disable static caching for the request and fall back to native caching logic. The cache key will still be returned, but a static file won't be written.

Step 3 - Configure your server

Your host needs to check for matching static files before Craft handles the request. If the file exists, it's served statically. This block should typically be set immediately preceding the primary Craft "index.php" rewrite. Use these examples as a general guideline, your implementation may vary., (*11)


# Check Presto cache
RewriteCond %{REQUEST_FILENAME} !\.(css|eot|gif|ico|jpe?g|otf|png|svg|ttf|webp|woff2?)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/presto%{REQUEST_URI}/index.html -f
RewriteRule .* /cache/%{HTTP_HOST}/presto%{REQUEST_URI}/index.html [L,E=nocache:1]]

# Craft rewrite here

If you add a cache group, you'll need to add additional configuration. Below is an example of a pjax implementation:, (*12)

RewriteCond %{REQUEST_FILENAME} !\.(css|eot|gif|ico|jpe?g|otf|png|svg|ttf|webp|woff2?)$ [NC]
RewriteCond %{HTTP:X-PJAX} true
RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/presto/pjax%{REQUEST_URI}/index.html -f
RewriteRule .* /cache/%{HTTP_HOST}/presto/pjax%{REQUEST_URI}/index.html [L,E=nocache:1]]


# Block direct cache access
location /cache {

# Check Presto cache
location ~ !\.(css|gif|ico|jpe?g|png|svg)$ {
    if ($request_method = GET) {
        try_files $uri /cache/$http_host/presto/$uri/index.html;

# Craft rewrite here

Disable Caching


If you use a multi-environment config, set an arbitrary cache variable in your general config. Override this variable on environments where you don't want static caching (e.g. local development)., (*13)

General Config Variable, (*14)

`cacheEnabled` => true

Cache Tag, (*15)

{% cache globally using key craft.presto.cache if conf.cacheEnabled is defined and conf.cacheEnabled %}
    {# Template Logic #}
{% endcache %}

Individual Templates

When using Presto the for, until, if, and unless parameters won't be respected on each request once the static html file is created. To disable the cache on individual templates, set a variable on the main cache tag. Override that variable on each template where you don't want static caching., (*16)

Cache Tag, (*17)

{% cache globally using key craft.presto.cache if cacheEnabled is defined ? cacheEnabled : true %}
    <!doctype html>
            {{ block('content') }}
{% endcache %}

Cache Template Override, (*18)

{% extends '[layout-template-path]' %}

{# Disable caching on this page #}
{% set cacheEnabled = false %}

{% block content %}
    {# page content #}
{% endblock %}

Directory Structure

Presto resolves subdomain hosts automatically. Static html files are created inside a directory named after the requested host (i.e. coolwebsite.com, sub.coolwebsite.com). An additional directory called "presto" is created inside each host directory to avoid .htaccess filename conflicts. See below for an example cache file directory structure:, (*19)

- cache
    - coolwebsite.com
        - presto
            - index.html
            - blog
                - index.html

Purging the Cache

To purge the cache, navigate to the Presto plugin settings page (Settings > Presto) and click "Purge Cache" (immediate) or "Schedule Purge" (cron)., (*20)

presto-settings, (*21)

Note: The Cron purge method does not clear the template cache. Remember to purge the template cache before you schedule a purge., (*22)

Purge Method

Presto provides two purge methods: immediate and cron., (*23)

Immediate Purge

By default, Presto will purge the static cache and all related Craft template caches immediately. This only occurs in the server instance where the cache was cleared., (*24)

Cron Purge

If you run Presto in an environment that spins up multiple server instances, set the purgeMethod config to "cron". Set up a cron job on each server instance that runs the presto/default/check console command. The following example will run it every 10 minutes., (*25)

*/10 * * * *  php /var/www/craft presto/default/check

Disabled/Archived Entries

If an entry exists in the CMS but is not displayed on the site (e.g. status is disabled, entry is archived, etc.), enabling the entry will not clear any caches. Presto only clears related entries that are displayed on the site. In order to display your newly enabled entry, purge the entire cache., (*26)


Presto comes with a couple of events should you need them., (*27)

The following events will emit a CacheEvent event handler with the following properties, (*28)

  • html - the generated HTML
  • cacheKey - the cache key
  • filePath - the file path that the static file will be saved
  • host - the hostname
  • path - the url segment path
  • config - any configuration passed from the PrestoVariable

The following events will emit a PurgeEVent event with the following parameters:, (*29)

  • cacheKey - an array of cache keys that were purged

Note: the purge all events will not pass through the cache keys, (*30)


  • Display a list of cached pages in the admin
  • Add ability to clear individual cached pages in the admin
  • Warm cache after an entry is saved or created


Copyright 2017 Lewis Communications, LLC. Licensed under the Apache License, Version 2.0., (*31)

Brought to you by Lewis Communications, (*32)

The Versions

24/07 2018



A static cache extension for Craft chaching

  Sources   Download


The Requires


cms craftcms craft-plugin craft presto

19/05 2018


A static cache extension for Craft chaching

  Sources   Download


The Requires


cms craftcms craft-plugin craft presto

19/05 2018



A static cache extension for Craft chaching

  Sources   Download


The Requires


cms craftcms craft-plugin craft presto