Skip to content

Commit b766203

Browse files
perf(laravel): Caching tables and indexes to avoid multiple queries
1 parent ac6f667 commit b766203

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Laravel/Eloquent/Metadata/ModelMetadata.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Database\Eloquent\Model;
1717
use Illuminate\Database\Eloquent\Relations\Relation;
1818
use Illuminate\Support\Collection;
19+
use Illuminate\Support\Facades\Cache;
1920
use Illuminate\Support\Facades\Gate;
2021
use Illuminate\Support\Str;
2122

@@ -65,8 +66,12 @@ public function getAttributes(Model $model): Collection
6566
$connection = $model->getConnection();
6667
$schema = $connection->getSchemaBuilder();
6768
$table = $model->getTable();
68-
$columns = $schema->getColumns($table);
69-
$indexes = $schema->getIndexes($table);
69+
$columns = Cache::flexible('api-platform.tables.'.$table, [5, 10], function () use ($schema, $table) {
70+
return $schema->getColumns($table);
71+
});
72+
$indexes = Cache::flexible('api-platform.indexes.'.$table, [5, 10], function () use ($schema, $table) {
73+
return $schema->getIndexes($table);
74+
});
7075

7176
return collect($columns)
7277
->map(fn ($column) => [

0 commit comments

Comments
 (0)