AugustMiller / kirby-architect

📐 Easily reference Blueprint data from anywhere in your Kirby application.
38 stars 1 forks source link

More info about the cache #5

Closed jenstornell closed 8 years ago

jenstornell commented 8 years ago

The plugin implements a static $blueprints property that acts as a cache for parsed Blueprints. Repeated reading and parsing of Blueprints has a major impact on performance.

How does it work? Is it refreshed every time a new blueprint file is used or is it a multilpe array? What is in the cache? The whole blueprint as array?

Reply here or in the docs. I just used my own blueprint repo but with 5 requests it's already noticeable slower (I don't have a cache).

I just want to make sure that I use your repo in a way that the cache is kept and not overwritten. Maybe it's all magic and I don't even need to think about it?

AugustMiller commented 8 years ago

The Architect class's static $cache property is populated per-request. It prevents the Architect::blueprint method from having to read Yaml off the disk every time one of the other helper functions requests a blueprint value.

Originally, the plugin was developed for the Coffee Variety Catalog, which outputs dozens of translated blueprint options per page (especially on a single variety), and it was having to re-read the same file off the disc as many times, parsing it each time!

The “cache” allows the server to read and parse the blueprint once and then refer to it from memory.

The combination of slow disk I/O and reducing the overhead of continually re-parsing hundreds of lines of Yaml brought the TTFB back down from 500–1000ms to a more comfortable (and normal) 50-60ms.

AugustMiller commented 8 years ago

I think the other half of this answer are these lines from the primary plugin file:

class Architect {
  # ...
  private static $blueprints = [];

  # Fetch all data for a Blueprint as an associative array
  public static function blueprint ($template) {
    if ( isset(static::$blueprints[$template]) ) {
      return static::$blueprints[$template];
    } else {
      return static::$blueprints[$template] = yaml::decode(kirby()->get('blueprint', $template));
    }
  }
  # ...
}

Before reading a blueprint from the disk/registry, we look in the static $cache property for a key matching the blueprint's name. If it hasn't been set, we parse the Yaml and save it to that key so it's accessible, later.