Skip to content

Commit

Permalink
fix cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3n committed Jan 23, 2022
1 parent b57f1bd commit 80d4fc0
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ tasks:
exec:
cmds:
- docker-compose -f docker-compose.yml exec {{.CLI_ARGS}}
symfony:
cmds:
- docker-compose -f docker-compose.yml exec app bash -c "php bin/console {{.CLI_ARGS}}"
reset:
cmds:
- docker-compose -f docker-compose.yml exec app bash -c "php bin/console semart:reset"
Expand Down
58 changes: 58 additions & 0 deletions lib/EventSubscriber/InvalidateDoctrineCacheSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace KejawenLab\ApiSkeleton\EventSubscriber;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

/**
* @author Muhamad Surya Iksanudin<[email protected]>
*/
final class InvalidateDoctrineCacheSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}

public function invalidate(KernelEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}

$request = $event->getRequest();
if ($request->isMethod(Request::METHOD_GET)) {
return;
}

if ($request->isMethod(Request::METHOD_HEAD)) {
return;
}

if ($request->isMethod(Request::METHOD_OPTIONS)) {
return;
}

$configuration = $this->entityManager->getConfiguration();

$configuration->getQueryCache()->clear();
$configuration->getResultCache()->clear();
}

/**
* @return array<string, string>
*/
public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => [['invalidate', 27]],
ResponseEvent::class => [['invalidate', -27]],
];
}
}
7 changes: 7 additions & 0 deletions lib/EventSubscriber/LogoutSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace KejawenLab\ApiSkeleton\EventSubscriber;

use Doctrine\ORM\EntityManagerInterface;
use KejawenLab\ApiSkeleton\Admin\AdminContext;
use KejawenLab\ApiSkeleton\Setting\Model\SettingInterface;
use Psr\Cache\CacheItemPoolInterface;
Expand All @@ -20,6 +21,7 @@ final class LogoutSubscriber implements EventSubscriberInterface
public function __construct(
private readonly UrlGeneratorInterface $urlGenerator,
private readonly CacheItemPoolInterface $cache,
private readonly EntityManagerInterface $entityManager,
) {
}

Expand All @@ -35,6 +37,11 @@ public function redirect(LogoutEvent $event): void
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE);
$this->cache->deleteItem(SettingInterface::CACHE_ID_MAX_API_PER_USER);

$configuration = $this->entityManager->getConfiguration();

$configuration->getQueryCache()->clear();
$configuration->getResultCache()->clear();

$event->setResponse(new RedirectResponse($this->urlGenerator->generate(AdminContext::ADMIN_ROUTE)));
}

Expand Down
17 changes: 17 additions & 0 deletions lib/EventSubscriber/SingleLoginSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace KejawenLab\ApiSkeleton\EventSubscriber;

use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use KejawenLab\ApiSkeleton\Admin\AdminContext;
use KejawenLab\ApiSkeleton\ApiClient\Model\ApiClientInterface;
use KejawenLab\ApiSkeleton\Security\Model\UserInterface;
Expand Down Expand Up @@ -32,6 +33,7 @@ public function __construct(
private readonly UserService $service,
private readonly UserProviderFactory $userProviderFactory,
private readonly CacheItemPoolInterface $cache,
private readonly EntityManagerInterface $entityManager,
) {
}

Expand Down Expand Up @@ -72,6 +74,11 @@ public function decode(JWTDecodedEvent $event): void
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE);
$this->cache->deleteItem(SettingInterface::CACHE_ID_MAX_API_PER_USER);

$configuration = $this->entityManager->getConfiguration();

$configuration->getQueryCache()->clear();
$configuration->getResultCache()->clear();

return;
}

Expand All @@ -92,6 +99,11 @@ public function decode(JWTDecodedEvent $event): void
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE_FIELD);
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE);
$this->cache->deleteItem(SettingInterface::CACHE_ID_MAX_API_PER_USER);

$configuration = $this->entityManager->getConfiguration();

$configuration->getQueryCache()->clear();
$configuration->getResultCache()->clear();
}

public function create(JWTCreatedEvent $event): void
Expand Down Expand Up @@ -122,6 +134,11 @@ public function create(JWTCreatedEvent $event): void
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE_FIELD);
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE);
$this->cache->deleteItem(SettingInterface::CACHE_ID_MAX_API_PER_USER);

$configuration = $this->entityManager->getConfiguration();

$configuration->getQueryCache()->clear();
$configuration->getResultCache()->clear();
}

/**
Expand Down
6 changes: 6 additions & 0 deletions lib/Security/AdminAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace KejawenLab\ApiSkeleton\Security;

use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use KejawenLab\ApiSkeleton\Admin\AdminContext;
use KejawenLab\ApiSkeleton\Security\Model\UserInterface;
use KejawenLab\ApiSkeleton\Security\Service\UserProviderFactory;
Expand Down Expand Up @@ -35,6 +36,7 @@ public function __construct(
private readonly UserProviderFactory $userProviderFactory,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly CacheItemPoolInterface $cache,
private readonly EntityManagerInterface $entityManager,
) {
}

Expand Down Expand Up @@ -82,6 +84,10 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE_FIELD);
$this->cache->deleteItem(SettingInterface::CACHE_ID_PER_PAGE);
$this->cache->deleteItem(SettingInterface::CACHE_ID_MAX_API_PER_USER);
$configuration = $this->entityManager->getConfiguration();

$configuration->getQueryCache()->clear();
$configuration->getResultCache()->clear();

return $this->redirect($session, $firewallName);
}
Expand Down

0 comments on commit 80d4fc0

Please sign in to comment.