Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(laravel): Caching tables and indexes to avoid multiple queries #6744

Open
wants to merge 6 commits into
base: 4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Laravel/Eloquent/Metadata/ModelMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -65,8 +66,14 @@ public function getAttributes(Model $model): Collection
$connection = $model->getConnection();
$schema = $connection->getSchemaBuilder();
$table = $model->getTable();
$columns = $schema->getColumns($table);
$indexes = $schema->getIndexes($table);
/** @var array<string, mixed> $columns */
$columns = Cache::flexible('api-platform.tables.'.$table, [5, 10], function () use ($schema, $table) {
return $schema->getColumns($table);
});
/** @var array<int, mixed> $indexes */
$indexes = Cache::flexible('api-platform.indexes.'.$table, [5, 10], function () use ($schema, $table) {
return $schema->getIndexes($table);
});
Copy link
Member

@soyuka soyuka Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting change, note also #6735. We already have a cache layer so that this isn't called more then once per metadata computation in production mode.

This cache is stored in memory ? File ? How do you make sure it's not stale ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soyuka the storage engine of the cache is configurable from laravel's cache config in the file config/cache.php. You can use any cache driver (file, database, redis, memcached, etc) and the second argument to the function [5, 10] basically tells laravel that the cache is only good for 5 seconds and between 5 to 10 seconds period refresh the cached value automatically so it'll only be at max 5 to 10 seconds old.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soyuka I logged all the queries and saw multiple calls to the information_schema table for the same table's columns and data and thats why I added this


return collect($columns)
->map(fn ($column) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
Loading