Skip to content

Commit

Permalink
Send http request to some url to invalidate cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoran Kabic committed Mar 20, 2024
1 parent c82ffc8 commit 13ac7fa
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 9 deletions.
76 changes: 75 additions & 1 deletion src/SWP/Bundle/CoreBundle/Controller/ContentListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Doctrine\ORM\EntityManagerInterface;
use Exception;
use GuzzleHttp\Client;
use SWP\Bundle\ContentListBundle\Form\Type\ContentListType;
use SWP\Bundle\ContentListBundle\Services\ContentListServiceInterface;
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
Expand Down Expand Up @@ -50,6 +51,8 @@ class ContentListController extends AbstractController {
private EntityManagerInterface $entityManager;
private EventDispatcherInterface $eventDispatcher;
private FactoryInterface $factory;
private string $invalidationCacheUrl;
private string $invalidationToken;

/**
* @param ContentListRepositoryInterface $contentListRepository
Expand All @@ -67,7 +70,9 @@ public function __construct(
FormFactoryInterface $formFactory,
EntityManagerInterface $entityManager,
EventDispatcherInterface $eventDispatcher,
FactoryInterface $factory
FactoryInterface $factory,
string $invalidationCacheUrl,
string $invalidationToken,
) {
$this->contentListRepository = $contentListRepository;
$this->contentListItemRepository = $contentListItemRepository;
Expand All @@ -76,6 +81,45 @@ public function __construct(
$this->entityManager = $entityManager;
$this->eventDispatcher = $eventDispatcher;
$this->factory = $factory;
$this->invalidationCacheUrl = $invalidationCacheUrl;
$this->invalidationToken = $invalidationToken;
}

public static function invalidateCache(string $url, string $token, array $data = [])
{
try {
$client = new Client();

$headers = [
'Content-Type' => 'application/json',
];
$queryParams = [
'secret' => $token,
];

$response = $client->request('POST', $url, [
'headers' => $headers,
'json' => $data,
'query' => $queryParams
]);
$responseBody = $response->getBody()->getContents();
$result = [
'request' => [
'headers' => $headers,
'json' => $data,
'query' => $queryParams
],
'response' => [
'status' => $response->getStatusCode(),
'body' => $responseBody,
'ReasonPhrase' => $response->getReasonPhrase()
]
];

file_put_contents('/tmp/cache_invalidation.json', json_encode($result) . PHP_EOL, FILE_APPEND);
} catch (\Throwable $e) {
file_put_contents('/tmp/cache_invalidation_errors.json', $e->getMessage() . PHP_EOL, FILE_APPEND);
}
}

/**
Expand Down Expand Up @@ -107,6 +151,16 @@ public function createAction(Request $request): SingleResourceResponseInterface

if ($form->isSubmitted() && $form->isValid()) {
$this->contentListRepository->add($contentList);
self::invalidateCache(
$this->invalidationCacheUrl,
$this->invalidationToken,
[
'id' => $contentList->getId(),
'name' => $contentList->getName(),
'type' => $contentList->getType(),
'action' => 'CREATE'
]
);

return new SingleResourceResponse($contentList, new ResponseContext(201));
}
Expand Down Expand Up @@ -134,6 +188,16 @@ public function updateAction(Request $request, int $id): SingleResourceResponseI
);

$objectManager->flush();
self::invalidateCache(
$this->invalidationCacheUrl,
$this->invalidationToken,
[
'id' => $contentList->getId(),
'name' => $contentList->getName(),
'type' => $contentList->getType(),
'action' => 'UPDATE'
]
);

return new SingleResourceResponse($contentList);
}
Expand All @@ -149,6 +213,16 @@ public function deleteAction($id): SingleResourceResponseInterface {
$contentList = $this->findOr404($id);

$repository->remove($contentList);
self::invalidateCache(
$this->invalidationCacheUrl,
$this->invalidationToken,
[
'id' => $contentList->getId(),
'name' => $contentList->getName(),
'type' => $contentList->getType(),
'action' => 'DELETE'
]
);

return new SingleResourceResponse(null, new ResponseContext(204));
}
Expand Down
43 changes: 35 additions & 8 deletions src/SWP/Bundle/CoreBundle/Controller/ContentListItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
use SWP\Bundle\ContentListBundle\Form\Type\ContentListItemsType;
use SWP\Bundle\ContentListBundle\Services\ContentListServiceInterface;
use SWP\Bundle\CoreBundle\Controller\ContentListController;
use SWP\Bundle\CoreBundle\Form\Type\ContentListItemType;
use SWP\Bundle\CoreBundle\Model\ContentListInterface;
use SWP\Bundle\CoreBundle\Model\ContentListItemInterface;
Expand Down Expand Up @@ -55,14 +56,20 @@ class ContentListItemController extends AbstractController {
* @param ContentListServiceInterface $contentListService
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(ContentListItemRepositoryInterface $contentListItemRepository,
EntityManagerInterface $entityManager,
ContentListServiceInterface $contentListService,
EventDispatcherInterface $eventDispatcher) {
$this->contentListItemRepository = $contentListItemRepository;
$this->entityManager = $entityManager;
$this->contentListService = $contentListService;
$this->eventDispatcher = $eventDispatcher;
public function __construct(
ContentListItemRepositoryInterface $contentListItemRepository,
EntityManagerInterface $entityManager,
ContentListServiceInterface $contentListService,
EventDispatcherInterface $eventDispatcher,
string $invalidationCacheUrl,
string $invalidationToken,
) {
$this->contentListItemRepository = $contentListItemRepository;
$this->entityManager = $entityManager;
$this->contentListService = $contentListService;
$this->eventDispatcher = $eventDispatcher;
$this->invalidationCacheUrl = $invalidationCacheUrl;
$this->invalidationToken = $invalidationToken;
}


Expand Down Expand Up @@ -137,6 +144,16 @@ public function updateAction(Request $request, FormFactoryInterface $formFactory
}

$this->entityManager->flush();
ContentListController::invalidateCache(
$this->invalidationCacheUrl,
$this->invalidationToken,
[
'id' => $contentListItem->getContentList()->getId(),
'name' => $contentListItem->getContentList()->getName(),
'type' => $contentListItem->getContentList()->getType(),
'action' => 'CREATE'
]
);

return new SingleResourceResponse($contentListItem);
}
Expand Down Expand Up @@ -244,6 +261,16 @@ public function batchUpdateAction(
ArticleEvents::POST_UPDATE
), ArticleEvents::POST_UPDATE);
}
ContentListController::invalidateCache(
$this->invalidationCacheUrl,
$this->invalidationToken,
[
'id' => $list->getId(),
'name' => $list->getName(),
'type' => $list->getType(),
'action' => 'BATCH-UPDATE'
]
);

return new SingleResourceResponse($list, new ResponseContext(201));
}
Expand Down
2 changes: 2 additions & 0 deletions src/SWP/Bundle/CoreBundle/Resources/config/controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
$cachedTenantContext: '@swp_multi_tenancy.tenant_context'
$packageRepository: '@swp.repository.package'
$routeRepository: '@swp.repository.route'
$invalidationCacheUrl: '%env(INVALIDATION_CACHE_URL)%'
$invalidationToken: '%env(INVALIDATION_CACHE_TOKEN)%'

SWP\Bundle\SettingsBundle\Controller\SettingsController: ~

Expand Down

0 comments on commit 13ac7fa

Please sign in to comment.