From 960aff0e403bf85d10652f5adbeab1289a19840c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Wed, 10 Jun 2026 14:39:57 +0200 Subject: [PATCH] feat: add caching to ajax calendar requests --- .../Controller/AjaxCalendarController.php | 41 +++++++++++++++---- .../ep_products/Configuration/Services.yaml | 4 ++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxCalendarController.php b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxCalendarController.php index 377ff23f..afb10f6c 100644 --- a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxCalendarController.php +++ b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxCalendarController.php @@ -30,21 +30,31 @@ namespace EP\EpProducts\Controller; use EP\EpProducts\BpnConnect\ApiClient; use EP\EpProducts\Domain\Model\Hotel; use EP\EpProducts\Utility\DateUtility; +use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; class AjaxCalendarController extends ActionController { + private const CACHE_TTL = 3600; + /** * @var ApiClient */ protected $apiClient; /** - * @param ApiClient $apiClient + * @var FrontendInterface */ - public function __construct(ApiClient $apiClient) + protected $cache; + + /** + * @param ApiClient $apiClient + * @param FrontendInterface $cache + */ + public function __construct(ApiClient $apiClient, FrontendInterface $cache) { $this->apiClient = $apiClient; + $this->cache = $cache; } /** @@ -70,11 +80,19 @@ class AjaxCalendarController extends ActionController $calendar['error'] = true; } else { try { - $calendar = $this->apiClient->getCalendar( - $hotelCode, - new \DateTimeImmutable($apiDateFrom), - new \DateTimeImmutable($apiDateTo) - ); + $cacheIdentifier = $this->getCacheIdentifier($hotelCode, $apiDateFrom, $apiDateTo); + $cachedCalendar = $this->cache->get($cacheIdentifier); + + if (false !== $cachedCalendar) { + $calendar = $cachedCalendar; + } else { + $calendar = $this->apiClient->getCalendar( + $hotelCode, + new \DateTimeImmutable($apiDateFrom), + new \DateTimeImmutable($apiDateTo) + ); + $this->cache->set($cacheIdentifier, $calendar, [], self::CACHE_TTL); + } } catch (\Throwable $e) { $calendar['error'] = true; } @@ -140,4 +158,13 @@ class AjaxCalendarController extends ActionController ]; } + private function getCacheIdentifier(string $hotelCode, string $dateFrom, string $dateTo): string + { + return 'bpn_connect_calendar_' . sha1(implode('|', [ + $hotelCode, + $dateFrom, + $dateTo, + ])); + } + } diff --git a/public/typo3conf/ext/ep_products/Configuration/Services.yaml b/public/typo3conf/ext/ep_products/Configuration/Services.yaml index 999f8f4a..f146a82c 100644 --- a/public/typo3conf/ext/ep_products/Configuration/Services.yaml +++ b/public/typo3conf/ext/ep_products/Configuration/Services.yaml @@ -27,3 +27,7 @@ services: EP\EpProducts\Service\ProductImageService: arguments: $cache: '@cache.ep_cache' + + EP\EpProducts\Controller\AjaxCalendarController: + arguments: + $cache: '@cache.ep_cache'