add cache to workflow counter

This commit is contained in:
Julien Fastré 2022-02-02 23:19:59 +01:00
parent 796b4ff76b
commit 211ecf1c3e
2 changed files with 66 additions and 6 deletions

View File

@ -110,7 +110,8 @@ class EntityWorkflowRepository implements ObjectRepository
$qb->where( $qb->where(
$qb->expr()->andX( $qb->expr()->andX(
$qb->expr()->isMemberOf(':user', 'step.destUser'), $qb->expr()->isMemberOf(':user', 'step.destUser'),
$qb->expr()->isNull('step.transitionAfter') $qb->expr()->isNull('step.transitionAfter'),
$qb->expr()->eq('step.isFinal', "'FALSE'")
) )
); );

View File

@ -12,30 +12,89 @@ declare(strict_types=1);
namespace Chill\MainBundle\Workflow\Counter; namespace Chill\MainBundle\Workflow\Counter;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepRepository; use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepRepository;
use Chill\MainBundle\Templating\UI\NotificationCounterInterface; use Chill\MainBundle\Templating\UI\NotificationCounterInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Security\Core\User\UserInterface; 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; private EntityWorkflowStepRepository $workflowStepRepository;
public function __construct(EntityWorkflowStepRepository $workflowStepRepository) private CacheItemPoolInterface $cacheItemPool;
public function __construct(EntityWorkflowStepRepository $workflowStepRepository, CacheItemPoolInterface $cacheItemPool)
{ {
$this->workflowStepRepository = $workflowStepRepository; $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 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 public function getCountUnreadByUser(User $user): int
{ {
return $this->workflowStepRepository->countUnreadByUser($user); 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',
];
}
} }