Skip to content

Commit bd20ba6

Browse files
committed
perf: Cache snippets
1 parent fb3100c commit bd20ba6

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/Cms/App.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public function __construct(array $props = [], bool $setInstance = true)
102102
$this->core = new Core($this);
103103
$this->events = new Events($this);
104104

105-
// start with a fresh version cache
105+
// start with a fresh snippet and version cache
106+
Snippet::$cache = [];
106107
VersionCache::reset();
107108

108109
// register all roots to be able to load stuff afterwards

src/Template/Snippet.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
*/
2424
class Snippet extends Tpl
2525
{
26+
/**
27+
* Cache for the snippet files
28+
*/
29+
final public static array $cache = [];
30+
2631
/**
2732
* Cache for the currently active
2833
* snippet. This is used to start
@@ -182,18 +187,25 @@ public static function file(string|array $name): string|null
182187

183188
foreach ($names as $name) {
184189
$name = (string)$name;
190+
191+
// retrieve the file from the cache if it exists
192+
if (isset(static::$cache[$name]) === true) {
193+
return static::$cache[$name];
194+
}
195+
185196
$file = $root . '/' . $name . '.php';
186197

187198
if (F::exists($file, $root) === false) {
188199
$file = $kirby->extensions('snippets')[$name] ?? null;
189200
}
190201

191202
if ($file) {
192-
break;
203+
// cache the file for future use
204+
return static::$cache[$name] = $file;
193205
}
194206
}
195207

196-
return $file;
208+
return null;
197209
}
198210

199211
/**

0 commit comments

Comments
 (0)