From 211ecf1c3e100be6f14c8add52850d8e0a367f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 2 Feb 2022 23:19:59 +0100 Subject: [PATCH] add cache to workflow counter --- .../Workflow/EntityWorkflowRepository.php | 3 +- .../Counter/WorkflowByUserCounter.php | 69 +++++++++++++++++-- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php b/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php index cac5c9996..63e27c7ba 100644 --- a/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/Workflow/EntityWorkflowRepository.php @@ -110,7 +110,8 @@ class EntityWorkflowRepository implements ObjectRepository $qb->where( $qb->expr()->andX( $qb->expr()->isMemberOf(':user', 'step.destUser'), - $qb->expr()->isNull('step.transitionAfter') + $qb->expr()->isNull('step.transitionAfter'), + $qb->expr()->eq('step.isFinal', "'FALSE'") ) ); diff --git a/src/Bundle/ChillMainBundle/Workflow/Counter/WorkflowByUserCounter.php b/src/Bundle/ChillMainBundle/Workflow/Counter/WorkflowByUserCounter.php index 7c1717d52..6a61e7de4 100644 --- a/src/Bundle/ChillMainBundle/Workflow/Counter/WorkflowByUserCounter.php +++ b/src/Bundle/ChillMainBundle/Workflow/Counter/WorkflowByUserCounter.php @@ -12,30 +12,89 @@ declare(strict_types=1); namespace Chill\MainBundle\Workflow\Counter; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Entity\Workflow\EntityWorkflow; use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepRepository; use Chill\MainBundle\Templating\UI\NotificationCounterInterface; +use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Component\Workflow\Event\Event; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; -final class WorkflowByUserCounter implements NotificationCounterInterface +final class WorkflowByUserCounter implements NotificationCounterInterface, EventSubscriberInterface { private EntityWorkflowStepRepository $workflowStepRepository; - public function __construct(EntityWorkflowStepRepository $workflowStepRepository) + private CacheItemPoolInterface $cacheItemPool; + + public function __construct(EntityWorkflowStepRepository $workflowStepRepository, CacheItemPoolInterface $cacheItemPool) { $this->workflowStepRepository = $workflowStepRepository; + $this->cacheItemPool = $cacheItemPool; } - public function addNotification(UserInterface $u): int + public function addNotification(UserInterface $user): int { - if (!$u instanceof User) { + if (!$user instanceof User) { return 0; } - return $this->getCountUnreadByUser($u); + $key = self::generateCacheKeyWorkflowByUser($user); + $item = $this->cacheItemPool->getItem($key); + + if ($item->isHit()) { + return $item->get(); + } + + $nb = $this->getCountUnreadByUser($user); + + $item->set($nb) + ->expiresAfter(60 * 15); + $this->cacheItemPool->save($item); + + return $nb; } public function getCountUnreadByUser(User $user): int { return $this->workflowStepRepository->countUnreadByUser($user); } + + public static function generateCacheKeyWorkflowByUser(User $user): string + { + return 'chill_main_workflow_by_u_'.$user->getId(); + } + + public function resetWorkflowCache(Event $event): void + { + if (!$event->getSubject() instanceof EntityWorkflow) { + return; + } + + /** @var EntityWorkflow $entityWorkflow */ + $entityWorkflow = $event->getSubject(); + $step = $entityWorkflow->getCurrentStep(); + + if ($step === null) { + return; + } + + $keys = []; + foreach ($step->getDestUser() as $user) { + $keys[] = self::generateCacheKeyWorkflowByUser($user); + } + + if ([] !== $keys) { + $this->cacheItemPool->deleteItems($keys); + } + + } + + public static function getSubscribedEvents() + { + return [ + 'workflow.leave' => 'resetWorkflowCache', + ]; + } + + }