-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1393 from MTES-MCT/feature/1320-cache-data-dkpi
[BO - Dashboard] Stratégie de cache sur les data-kpi du dashboard
- Loading branch information
Showing
16 changed files
with
464 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace App\EventSubscriber; | ||
|
||
use App\Entity\Notification; | ||
use App\Entity\Signalement; | ||
use App\Entity\User; | ||
use App\Service\CacheCommonKeyGenerator; | ||
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface; | ||
use Doctrine\ORM\Events; | ||
use Doctrine\Persistence\Event\LifecycleEventArgs; | ||
use Psr\Cache\InvalidArgumentException; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Bundle\SecurityBundle\Security; | ||
use Symfony\Contracts\Cache\TagAwareCacheInterface; | ||
|
||
class CacheInvalidationSubscriber implements EventSubscriberInterface | ||
{ | ||
public const CONTEXT_WIDGET_DATA_KPI = 'countDataKpi'; | ||
|
||
public function __construct( | ||
readonly private TagAwareCacheInterface $dashboardCache, | ||
readonly private CacheCommonKeyGenerator $cacheCommonKeyGenerator, | ||
readonly private LoggerInterface $logger, | ||
private readonly Security $security, | ||
) { | ||
} | ||
|
||
public function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
Events::postPersist, | ||
Events::postUpdate, | ||
Events::postRemove, | ||
]; | ||
} | ||
|
||
public function postPersist(LifecycleEventArgs $args): void | ||
{ | ||
$this->invalidateCacheWidgetDataKpi($args); | ||
} | ||
|
||
public function postUpdate(LifecycleEventArgs $args): void | ||
{ | ||
$this->invalidateCacheWidgetDataKpi($args); | ||
} | ||
|
||
public function postRemove(LifecycleEventArgs $args): void | ||
{ | ||
$this->invalidateCacheWidgetDataKpi($args); | ||
} | ||
|
||
public function supports(mixed $entity): bool | ||
{ | ||
return $entity instanceof Signalement || $entity instanceof Notification; | ||
} | ||
|
||
private function invalidateCacheWidgetDataKpi(LifecycleEventArgs $args): void | ||
{ | ||
$entity = $args->getObject(); | ||
if ($this->supports($entity)) { | ||
/** @var User $user */ | ||
$user = $this->security->getUser(); | ||
$territory = $user?->getTerritory(); | ||
try { | ||
if ($entity instanceof Signalement) { | ||
$this->dashboardCache->invalidateTags(['data-kpi-'.$territory?->getZip()]); | ||
} else { | ||
$commonKey = $this->cacheCommonKeyGenerator->generate(); | ||
$key = self::CONTEXT_WIDGET_DATA_KPI | ||
.'-'.$commonKey | ||
.'-zip-'.$territory?->getZip() | ||
.'-id-'.$user?->getId(); | ||
$this->dashboardCache->delete($key); | ||
} | ||
} catch (InvalidArgumentException $exception) { | ||
$this->logger->error(sprintf('Invalidate cache failed %s', $exception->getMessage())); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Service; | ||
|
||
use App\Entity\User; | ||
use Symfony\Bundle\SecurityBundle\Security; | ||
|
||
class CacheCommonKeyGenerator | ||
{ | ||
public function __construct(private readonly Security $security) | ||
{ | ||
} | ||
|
||
public function generate(): ?string | ||
{ | ||
/** @var User $user */ | ||
$user = $this->security->getUser(); | ||
$role = $user?->getRoles(); | ||
|
||
return \is_array($role) ? array_shift($role).'-partnerId-'.$user->getPartner()->getId() : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.