Skip to content

Commit f578871

Browse files
authored
Prevent n+1 queries on asset meta exists (#446)
* Prevent n+1 queries on asset meta exists * Change of approach * Use method thats available
1 parent 9350fd6 commit f578871

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Assets/Asset.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ public function syncOriginal()
3232

3333
public static function fromModel(Model $model)
3434
{
35-
return (new static)
35+
$asset = (new static)
3636
->container($model->container)
3737
->path(Str::replace('//', '/', $model->folder.'/'.$model->basename))
3838
->hydrateMeta($model->meta)
3939
->syncOriginal();
40+
41+
Blink::put('eloquent-asset-'.$asset->id(), $model);
42+
Blink::put($asset->metaCacheKey(), $model->meta);
43+
44+
return $asset;
4045
}
4146

4247
public function meta($key = null)
@@ -86,6 +91,10 @@ public function exists()
8691

8792
public function metaExists()
8893
{
94+
if (Blink::has($this->metaCacheKey())) {
95+
return true;
96+
}
97+
8998
return Blink::once('eloquent-asset-meta-exists-'.$this->id(), function () {
9099
return app('statamic.eloquent.assets.model')::query()
91100
->where([
@@ -131,6 +140,7 @@ public function writeMeta($meta)
131140

132141
self::makeModelFromContract($this, $meta)?->save();
133142

143+
Blink::put($this->metaCacheKey(), $meta);
134144
Blink::put('eloquent-asset-meta-exists-'.$this->id(), true);
135145
}
136146

0 commit comments

Comments
 (0)