Caching

In order to make your site run fast, Wireframe provides a number of features related to caching: some are fully automatic, while others need to be specifically enabled. On this page we're going to introduce all the major caching features in Wireframe.

There are basically two types of caches in Wireframe: automatic and transient caches, and manual and persistent caches. Automatic caches are unobtrusive and should only affect a single web request, while manual caches use WireCache behind the scenes and need to be intentionally enabled.

As of this writing the only persistent cache is for public Controller method return values, but more will likely follow.

Automatic runtime caching of public Controller method return values

When you request a public method from a Controller (in the Controller class itself, or in a view, layout, or partial file), the return value is automatically cached in a transient runtime cache. This is quite important in case a Controller method needs to perform an expensive calculation, fetch data from an outside source, etc. — you'd rarely want to risk repeating such actions multiple times for a single page render.

Now, if this cache is problematic for your use case — i.e. you actually need some method to always return a fresh, uncached value — you can prevent it by adding the method name to the $uncacheable_methods property of your Controller class:

class MyController {

    protected $uncacheable_methods = [
        'my_rand_value',
    ];

    public function my_rand_value(): int {
        // return a fresh random integer every single time!
        return rand();
    }

}

Manual persistent caching of public Controller method return values

In addition to aforementioned automatic runtime caching for Controller method return values, Wireframe also supports persistent caching for Controller method return values. While transient caching is opt-out, persistent caching is (for obvious reasons) opt-in:

class MyController {

    protected $cacheable_methods = [
        'my_rand_value' => 3600,
    ];

    public function my_rand_value(): int {
        // this will return a random integer, which is then cached for an hour
        return rand();
    }

}

The persistent caching method used behind the scenes is WireCache ($cache API variable), which means that cached values are stored in the database. Cache key gets automatically generated based on controller name, method name, and page ID:

$cache_name = 'wireframe'
            . '/controller=' . static::class
            . '/method=' . $name
            . '/page=' . $this->wire('page');

Automatic runtime caching of rendered page views

When you ask Wireframe to render a page for you, the return value (i.e. rendered page content) is automatically stored in transient runtime cache. This means that if you, say, render a page multiple times in a row, all the heavy lifting is done only once, while subsequent render calls are returned from an internal cache.

This cache is maintained within the Wireframe module instance itself. In order to avoid returning unexpected values from the cache, Wireframe automatically generates a unique cache key using...

This cache was added in Wireframe 0.5.0.

Automatic runtime caching of the list of partials (partial objects)

When you init Wireframe, a list of partial files is automatically loaded into memory. This list — which consists of Partials objects containing Partial objects (and, potentially, additional nested Partials objects) — is stored into $partials property of the View object.

This cache shouldn't have any real world implications, other than speeding things up internally: memory consumption is low, and only situation where this could be a problem would be if you needed to add new partial files runtime (which seems like a rare occurrence, to say the least).

Back to top