From b7662033225d2dfdf87c71ceb91b99e1ca017639 Mon Sep 17 00:00:00 2001 From: Amer Chaudhary Date: Wed, 23 Oct 2024 17:17:54 +0000 Subject: [PATCH 1/6] perf(laravel): Caching tables and indexes to avoid multiple queries --- src/Laravel/Eloquent/Metadata/ModelMetadata.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Laravel/Eloquent/Metadata/ModelMetadata.php b/src/Laravel/Eloquent/Metadata/ModelMetadata.php index 3d851681c0..5b4c9c64a5 100644 --- a/src/Laravel/Eloquent/Metadata/ModelMetadata.php +++ b/src/Laravel/Eloquent/Metadata/ModelMetadata.php @@ -16,6 +16,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Str; @@ -65,8 +66,12 @@ public function getAttributes(Model $model): Collection $connection = $model->getConnection(); $schema = $connection->getSchemaBuilder(); $table = $model->getTable(); - $columns = $schema->getColumns($table); - $indexes = $schema->getIndexes($table); + $columns = Cache::flexible('api-platform.tables.'.$table, [5, 10], function () use ($schema, $table) { + return $schema->getColumns($table); + }); + $indexes = Cache::flexible('api-platform.indexes.'.$table, [5, 10], function () use ($schema, $table) { + return $schema->getIndexes($table); + }); return collect($columns) ->map(fn ($column) => [ From cc206e8a59af0123974644392b9baf217247d314 Mon Sep 17 00:00:00 2001 From: Amer Chaudhary Date: Wed, 23 Oct 2024 18:02:10 +0000 Subject: [PATCH 2/6] perf(laravel) Updates WorkbenchServiceProvider to use null for the default cache driver --- src/Laravel/workbench/app/Providers/WorkbenchServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Laravel/workbench/app/Providers/WorkbenchServiceProvider.php b/src/Laravel/workbench/app/Providers/WorkbenchServiceProvider.php index a6237450d1..eb0d9f4d5c 100644 --- a/src/Laravel/workbench/app/Providers/WorkbenchServiceProvider.php +++ b/src/Laravel/workbench/app/Providers/WorkbenchServiceProvider.php @@ -32,6 +32,7 @@ public function register(): void { $config = $this->app['config']; $config->set('api-platform.resources', [app_path('Models'), app_path('ApiResource')]); + $config->set('cache.default', 'null'); } /** From 898ab51c6e02198627c9e0391979401b945643ba Mon Sep 17 00:00:00 2001 From: Amer Chaudhary Date: Wed, 23 Oct 2024 18:25:11 +0000 Subject: [PATCH 3/6] perf(laravel): making phpstan happy --- src/Laravel/Eloquent/Metadata/ModelMetadata.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Laravel/Eloquent/Metadata/ModelMetadata.php b/src/Laravel/Eloquent/Metadata/ModelMetadata.php index 5b4c9c64a5..62f1b7839b 100644 --- a/src/Laravel/Eloquent/Metadata/ModelMetadata.php +++ b/src/Laravel/Eloquent/Metadata/ModelMetadata.php @@ -66,9 +66,11 @@ public function getAttributes(Model $model): Collection $connection = $model->getConnection(); $schema = $connection->getSchemaBuilder(); $table = $model->getTable(); + /** @var array $columns */ $columns = Cache::flexible('api-platform.tables.'.$table, [5, 10], function () use ($schema, $table) { return $schema->getColumns($table); }); + /** @var array $indexes */ $indexes = Cache::flexible('api-platform.indexes.'.$table, [5, 10], function () use ($schema, $table) { return $schema->getIndexes($table); }); From b6068fc52b61b045c6ebbe10dbd17a3c86c9caa7 Mon Sep 17 00:00:00 2001 From: Amer Chaudhary Date: Wed, 23 Oct 2024 18:33:41 +0000 Subject: [PATCH 4/6] perf(laravel) fixing the return type for phpstan --- src/Laravel/Eloquent/Metadata/ModelMetadata.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Laravel/Eloquent/Metadata/ModelMetadata.php b/src/Laravel/Eloquent/Metadata/ModelMetadata.php index 62f1b7839b..520532a4e2 100644 --- a/src/Laravel/Eloquent/Metadata/ModelMetadata.php +++ b/src/Laravel/Eloquent/Metadata/ModelMetadata.php @@ -66,11 +66,11 @@ public function getAttributes(Model $model): Collection $connection = $model->getConnection(); $schema = $connection->getSchemaBuilder(); $table = $model->getTable(); - /** @var array $columns */ + /** @var array $columns */ $columns = Cache::flexible('api-platform.tables.'.$table, [5, 10], function () use ($schema, $table) { return $schema->getColumns($table); }); - /** @var array $indexes */ + /** @var array $indexes */ $indexes = Cache::flexible('api-platform.indexes.'.$table, [5, 10], function () use ($schema, $table) { return $schema->getIndexes($table); }); From 618785ac2e76981fecefbb381526271f64926f85 Mon Sep 17 00:00:00 2001 From: Amer Chaudhary Date: Wed, 23 Oct 2024 18:38:57 +0000 Subject: [PATCH 5/6] perf(laravel): changing the return type for phpstan --- src/Laravel/Eloquent/Metadata/ModelMetadata.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Laravel/Eloquent/Metadata/ModelMetadata.php b/src/Laravel/Eloquent/Metadata/ModelMetadata.php index 520532a4e2..dda1c34151 100644 --- a/src/Laravel/Eloquent/Metadata/ModelMetadata.php +++ b/src/Laravel/Eloquent/Metadata/ModelMetadata.php @@ -66,11 +66,11 @@ public function getAttributes(Model $model): Collection $connection = $model->getConnection(); $schema = $connection->getSchemaBuilder(); $table = $model->getTable(); - /** @var array $columns */ + /** @var array $columns */ $columns = Cache::flexible('api-platform.tables.'.$table, [5, 10], function () use ($schema, $table) { return $schema->getColumns($table); }); - /** @var array $indexes */ + /** @var array $indexes */ $indexes = Cache::flexible('api-platform.indexes.'.$table, [5, 10], function () use ($schema, $table) { return $schema->getIndexes($table); }); From a23ebcc48b1bda5ae98436334782628630d6d98b Mon Sep 17 00:00:00 2001 From: Amer Chaudhary Date: Wed, 23 Oct 2024 18:45:15 +0000 Subject: [PATCH 6/6] perf(laravel): making phpstan happier --- src/Laravel/Eloquent/Metadata/ModelMetadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Laravel/Eloquent/Metadata/ModelMetadata.php b/src/Laravel/Eloquent/Metadata/ModelMetadata.php index dda1c34151..42a3b87e1e 100644 --- a/src/Laravel/Eloquent/Metadata/ModelMetadata.php +++ b/src/Laravel/Eloquent/Metadata/ModelMetadata.php @@ -70,7 +70,7 @@ public function getAttributes(Model $model): Collection $columns = Cache::flexible('api-platform.tables.'.$table, [5, 10], function () use ($schema, $table) { return $schema->getColumns($table); }); - /** @var array $indexes */ + /** @var array $indexes */ $indexes = Cache::flexible('api-platform.indexes.'.$table, [5, 10], function () use ($schema, $table) { return $schema->getIndexes($table); });