feat: Optimiser la pagination avec cache-aside et ports de lecture dédiés
Les listes paginées (utilisateurs, classes, matières, affectations, invitations parents, droits à l'image) effectuaient des requêtes SQL complètes à chaque chargement de page, sans aucun cache. Sur les établissements avec plusieurs centaines d'enregistrements, cela causait des temps de réponse perceptibles et une charge inutile sur PostgreSQL. Cette refactorisation introduit un cache tag-aware (Redis en prod, filesystem en dev) avec invalidation événementielle, et extrait les requêtes de lecture dans des ports Application / implémentations DBAL conformes à l'architecture hexagonale. Un middleware Messenger garantit l'invalidation synchrone du cache même pour les événements routés en asynchrone (envoi d'emails), évitant ainsi toute donnée périmée côté UI.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Administration\Infrastructure\ReadModel;
|
||||
|
||||
use App\Administration\Application\Dto\PaginatedResult;
|
||||
use App\Administration\Application\Port\PaginatedClassesReader;
|
||||
use App\Administration\Application\Query\GetClasses\ClassDto;
|
||||
use App\Administration\Domain\Model\SchoolClass\ClassStatus;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\DBAL\Connection;
|
||||
|
||||
final readonly class DbalPaginatedClassesReader implements PaginatedClassesReader
|
||||
{
|
||||
public function __construct(
|
||||
private Connection $connection,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PaginatedResult<ClassDto>
|
||||
*/
|
||||
public function findPaginated(
|
||||
string $tenantId,
|
||||
string $academicYearId,
|
||||
?string $search,
|
||||
int $page,
|
||||
int $limit,
|
||||
): PaginatedResult {
|
||||
$params = [
|
||||
'tenant_id' => $tenantId,
|
||||
'academic_year_id' => $academicYearId,
|
||||
'status' => ClassStatus::ACTIVE->value,
|
||||
];
|
||||
$whereClause = 'sc.tenant_id = :tenant_id AND sc.academic_year_id = :academic_year_id AND sc.status = :status';
|
||||
|
||||
if ($search !== null && $search !== '') {
|
||||
$whereClause .= ' AND (sc.name ILIKE :search OR sc.level ILIKE :search)';
|
||||
$params['search'] = '%' . $search . '%';
|
||||
}
|
||||
|
||||
$countSql = "SELECT COUNT(*) FROM school_classes sc WHERE {$whereClause}";
|
||||
|
||||
/** @var int|string|false $totalRaw */
|
||||
$totalRaw = $this->connection->fetchOne($countSql, $params);
|
||||
$total = (int) $totalRaw;
|
||||
|
||||
$offset = ($page - 1) * $limit;
|
||||
|
||||
$selectSql = <<<SQL
|
||||
SELECT sc.id, sc.name, sc.level, sc.capacity, sc.status, sc.description, sc.created_at, sc.updated_at
|
||||
FROM school_classes sc
|
||||
WHERE {$whereClause}
|
||||
ORDER BY sc.name ASC
|
||||
LIMIT :limit OFFSET :offset
|
||||
SQL;
|
||||
|
||||
$params['limit'] = $limit;
|
||||
$params['offset'] = $offset;
|
||||
|
||||
$rows = $this->connection->fetchAllAssociative($selectSql, $params);
|
||||
|
||||
$items = array_map(static function (array $row): ClassDto {
|
||||
/** @var string $id */
|
||||
$id = $row['id'];
|
||||
/** @var string $name */
|
||||
$name = $row['name'];
|
||||
/** @var string|null $level */
|
||||
$level = $row['level'];
|
||||
/** @var int|string|null $capacityRaw */
|
||||
$capacityRaw = $row['capacity'];
|
||||
$capacity = $capacityRaw !== null ? (int) $capacityRaw : null;
|
||||
/** @var string $status */
|
||||
$status = $row['status'];
|
||||
/** @var string|null $description */
|
||||
$description = $row['description'];
|
||||
/** @var string $createdAt */
|
||||
$createdAt = $row['created_at'];
|
||||
/** @var string $updatedAt */
|
||||
$updatedAt = $row['updated_at'];
|
||||
|
||||
return new ClassDto(
|
||||
id: $id,
|
||||
name: $name,
|
||||
level: $level,
|
||||
capacity: $capacity,
|
||||
status: $status,
|
||||
description: $description,
|
||||
createdAt: new DateTimeImmutable($createdAt),
|
||||
updatedAt: new DateTimeImmutable($updatedAt),
|
||||
);
|
||||
}, $rows);
|
||||
|
||||
return new PaginatedResult(
|
||||
items: $items,
|
||||
total: $total,
|
||||
page: $page,
|
||||
limit: $limit,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user