feat: add caching to ajax calendar requests

This commit is contained in:
Björn Fromme
2026-06-10 14:42:49 +02:00
parent a5b526c5a3
commit 960aff0e40
2 changed files with 38 additions and 7 deletions
@@ -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 {
$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,
]));
}
}
@@ -27,3 +27,7 @@ services:
EP\EpProducts\Service\ProductImageService:
arguments:
$cache: '@cache.ep_cache'
EP\EpProducts\Controller\AjaxCalendarController:
arguments:
$cache: '@cache.ep_cache'