-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 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
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]], | ||
]; | ||
} | ||
} |
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