Skip to content

Commit

Permalink
⚡ even better caching
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Mar 22, 2024
1 parent e680e7f commit 151b5c4
Show file tree
Hide file tree
Showing 17 changed files with 3,239 additions and 45 deletions.
29 changes: 24 additions & 5 deletions classes/Blueprints/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,40 @@ public function __toString(): string
return Yaml::encode($this->toArray());
}

public function blueprintCacheKeyFromModel(): string
{
$ref = new ReflectionClass($this->modelClass);

if ($ref->isSubclassOf(\Kirby\Cms\Page::class)) {
return 'pages/'.strtolower(substr($this->modelClass, 0, -4));
} elseif ($ref->isSubclassOf(\Kirby\Cms\File::class)) {
return 'files/'.strtolower(substr($this->modelClass, 0, -4));
} elseif ($ref->isSubclassOf(\Kirby\Cms\User::class)) {
return 'users/'.strtolower(substr($this->modelClass, 0, -4));
}

return $this->modelClass;
}

public function toArray(): array
{
$blueprint = $this->cache ? BlueprintCache::get($this->modelClass, null, $this->cache) : null;
$key = $this->blueprintCacheKeyFromModel();
$blueprint = $this->cache ? BlueprintCache::get($key, null, $this->cache) : null;
if ($blueprint) {
return $blueprint;
return [$key => $blueprint];
}

$blueprint = self::getBlueprintUsingReflection($this->modelClass);
// get first value of array, the key of array matched the $key
$blueprint = reset($blueprint);

// some might not be cacheable like when they are class based and have dynamic fields
if ($this->cache) {
BlueprintCache::set($this->modelClass, $blueprint);
// only set here now if they will not be written on __destruct by trait
if ($this->cache && ! method_exists($this->modelClass, 'blueprintCacheKey')) {
BlueprintCache::set($key, $blueprint);
}

return $blueprint;
return [$key => $blueprint];
}

public static function getBlueprintFieldsFromReflection(string $class): array
Expand Down
40 changes: 19 additions & 21 deletions classes/Blueprints/BlueprintCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ public static function cacheDir(): ?string

protected static function cacheFile(string $key): ?string
{
$hash = hash('xxh3', $key);
$hash = substr($hash, 0, 2).'/'.substr($hash, 2);

return static::cacheDir() ?
static::cacheDir().'/'.$hash.'.cache'
static::cacheDir().'/'.$key.'.cache'
: null;
}

Expand Down Expand Up @@ -64,7 +61,14 @@ public static function exists(string $key, $expire = null): bool

public static function get(string $key, $default = null, $expire = null): ?array
{
/*
if ($loaded = \Kirby\Toolkit\A::get(\Kirby\Cms\Blueprint::$loaded, $key)) {
return $loaded;
}
*/

$file = static::cacheFile($key);

if ($file && static::exists($key, $expire)) {
return Json::read($file);
}
Expand All @@ -84,28 +88,22 @@ public static function getKey(): string
return 'bnomei.blueprints.cache.dir';
}

/*
public static function preloadCachedBlueprints(): void
{
$kirby = kirby();
$preload = $kirby->option('bnomei.blueprints.preload');
if ($preload === false) {
return;
}

$preloaded = [];
foreach ($preload as $type) {
$blueprints = $kirby->blueprints($type);
foreach ($blueprints as $name) {
$key = $type.'/'.$name;
$blueprint = static::get($key);
if ($blueprint === null) {
foreach (Dir::dirs(static::cacheDir(), [], true) as $dir) {
foreach (Dir::files($dir, [], true) as $file) {
if (! \Kirby\Toolkit\Str::endsWith($file, '.cache')) {
continue;
}
$preloaded[$key] = $blueprint;
$key = str_replace([static::cacheDir().'/', '.cache'], ['', ''], $file);
$blueprint = Json::read($file);
\Kirby\Cms\Blueprint::$loaded[$key] = $blueprint;
}
}
$kirby->extend([
'blueprints' => $preloaded,
]);
// ray('preloaded', \Kirby\Cms\Blueprint::$loaded);
return count(\Kirby\Cms\Blueprint::$loaded);
}
*/
}
12 changes: 8 additions & 4 deletions classes/Blueprints/HasBlueprintCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function blueprintCacheKey(): string
PageBlueprint::class => 'pages',
FileBlueprint::class => 'files',
UserBlueprint::class => 'users',
};
}.'/';

return $type.'/'.$blueprint->name();
return $type.str_replace($type, '', $blueprint->name());
}

public function __destruct()
Expand All @@ -34,12 +34,16 @@ public function __destruct()
if (BlueprintCache::exists($key) === false) {
$blueprint = $this->blueprint();
$data = $blueprint->toArray();
$copy = $data;
if (isset(static::$blueprintCacheResolve)) {
foreach ($blueprint->toArray()['tabs'] as $tabKey => $tab) {
foreach ($copy['tabs'] as $tabKey => $tab) {
foreach ($tab['columns'] as $columnKey => $column) {
foreach ($column['sections'] as $sectionKey => $section) {
$section = $blueprint->section($sectionKey);
$data['tabs'][$tabKey]['columns'][$columnKey]['sections'][$sectionKey]['fields'] = $section->toArray()['fields'];
$path = $data['tabs'][$tabKey]['columns'][$columnKey]['sections'][$sectionKey];
if (array_key_exists('fields', $path)) {
$path['fields'] = $section->toArray()['fields'];
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby-blueprints",
"type": "kirby-plugin",
"version": "4.4.0",
"version": "4.4.1",
"license": "MIT",
"homepage": "https://github.com/bnomei/kirby3-blueprints",
"description": "PHP Class-based Blueprints for Kirby CMS for better type safety and code completion",
Expand Down Expand Up @@ -51,7 +51,7 @@
"getkirby/composer-installer": "^1.2"
},
"require-dev": {
"bnomei/autoloader-for-kirby": "^4.2.1",
"bnomei/autoloader-for-kirby": "^4.2",
"getkirby/cms": "^4.0.0-beta.2",
"larastan/larastan": "^2.9",
"laravel/pint": "^1.11",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// and use it on next request
\Bnomei\Blueprints\BlueprintCache::rememberCacheDir();
\Bnomei\Blueprints\Blueprint::loadPluginsAfter();
\Bnomei\Blueprints\BlueprintCache::preloadCachedBlueprints();
},
],
]);
File renamed without changes.
4 changes: 3 additions & 1 deletion tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
require __DIR__.'/../vendor/autoload.php';

$micotime = microtime(true);
$render = (new Kirby())->render();
//\Bnomei\Blueprints\BlueprintCache::preloadFromLoadMap();
$kirby = new Kirby();
$render = $kirby->render();
$micotime = microtime(true) - $micotime;
header('X-Render-Time: '.number_format($micotime, 3, '.', '').'s');
echo $render;
7 changes: 7 additions & 0 deletions tests/site/models/caching.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

class CachingPage extends \Kirby\Cms\Page
{
use \Bnomei\Blueprints\HasBlueprintCache;
use \Bnomei\Blueprints\HasBlueprintCacheResolve;
}
2 changes: 1 addition & 1 deletion tests/site/models/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
class ExamplePage extends \Kirby\Cms\Page
{
use \Bnomei\Blueprints\HasBlueprintCache;
use \Bnomei\Blueprints\HasBlueprintCacheResolve;
//use \Bnomei\Blueprints\HasBlueprintCacheResolve;
}
Loading

0 comments on commit 151b5c4

Please sign in to comment.