Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ZMSKVR-138): Improve caching response cache mapping in second cache #910

Merged
merged 6 commits into from
Feb 28, 2025
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
*/
class ZmsApiFacadeService
{
private const CACHE_KEY_OFFICES = 'processed_offices';
private const CACHE_KEY_SCOPES = 'processed_scopes';
private const CACHE_KEY_SERVICES = 'processed_services';
private const CACHE_KEY_OFFICES_AND_SERVICES = 'processed_offices_and_services';
private const CACHE_KEY_OFFICES_BY_SERVICE_PREFIX = 'processed_offices_by_service_';
private const CACHE_KEY_SERVICES_BY_OFFICE_PREFIX = 'processed_services_by_office_';

private static ?string $currentLanguage = null;
public static function setLanguageContext(?string $language): void
{
Expand All @@ -45,8 +52,25 @@ private static function getError(string $key): array
return ErrorMessages::get($key, self::$currentLanguage);
}

private static function setMappedCache(string $cacheKey, mixed $data): void
{
if (\App::$cache) {
\App::$cache->set($cacheKey, $data, \App::$SOURCE_CACHE_TTL);
LoggerService::logInfo('Second-level cache set', [
'key' => $cacheKey,
'ttl' => \App::$SOURCE_CACHE_TTL
]);
}
}

public static function getOffices(bool $showUnpublished = false): OfficeList
{
$cacheKey = self::CACHE_KEY_OFFICES . ($showUnpublished ? '_unpublished' : '');

if (\App::$cache && ($cachedData = \App::$cache->get($cacheKey))) {
return $cachedData;
}

$providerList = ZmsApiClientService::getOffices() ?? new ProviderList();
$scopeList = ZmsApiClientService::getScopes() ?? new ScopeList();
$offices = [];
Expand Down Expand Up @@ -90,11 +114,21 @@ public static function getOffices(bool $showUnpublished = false): OfficeList
);
}

return new OfficeList($offices);
$result = new OfficeList($offices);

self::setMappedCache($cacheKey, $result);

return $result;
}

public static function getScopes(): ThinnedScopeList|array
{
$cacheKey = self::CACHE_KEY_SCOPES;

if (\App::$cache && ($cachedData = \App::$cache->get($cacheKey))) {
return $cachedData;
}

$providerList = ZmsApiClientService::getOffices() ?? new ProviderList();
$scopeList = ZmsApiClientService::getScopes() ?? new ScopeList();
$scopeMap = [];
Expand Down Expand Up @@ -128,11 +162,21 @@ public static function getScopes(): ThinnedScopeList|array
}
}

return new ThinnedScopeList($scopesProjectionList);
$result = new ThinnedScopeList($scopesProjectionList);

self::setMappedCache($cacheKey, $result);

return $result;
}

public static function getServices(bool $showUnpublished = false): ServiceList|array
{
$cacheKey = self::CACHE_KEY_SERVICES . ($showUnpublished ? '_unpublished' : '');

if (\App::$cache && ($cachedData = \App::$cache->get($cacheKey))) {
return $cachedData;
}

$requestList = ZmsApiClientService::getServices() ?? new RequestList();
$services = [];
foreach ($requestList as $request) {
Expand All @@ -148,11 +192,21 @@ public static function getServices(bool $showUnpublished = false): ServiceList|a
$services[] = new Service(id: (int) $request->getId(), name: $request->getName(), maxQuantity: $additionalData['maxQuantity'] ?? 1);
}

return new ServiceList($services);
$result = new ServiceList($services);

self::setMappedCache($cacheKey, $result);

return $result;
}

public static function getServicesAndOffices(bool $showUnpublished = false): OfficeServiceAndRelationList|array
{
$cacheKey = self::CACHE_KEY_OFFICES_AND_SERVICES . ($showUnpublished ? '_unpublished' : '');

if (\App::$cache && ($cachedData = \App::$cache->get($cacheKey))) {
return $cachedData;
}

$providerList = ZmsApiClientService::getOffices() ?? new ProviderList();
$requestList = ZmsApiClientService::getServices() ?? new RequestList();
$relationList = ZmsApiClientService::getRequestRelationList() ?? new RequestRelationList();
Expand All @@ -164,7 +218,12 @@ public static function getServicesAndOffices(bool $showUnpublished = false): Off
$showUnpublished
) ?? new ServiceList();
$relations = MapperService::mapRelations($relationList) ?? new OfficeServiceRelationList();
return new OfficeServiceAndRelationList($offices, $services, $relations);

$result = new OfficeServiceAndRelationList($offices, $services, $relations);

self::setMappedCache($cacheKey, $result);

return $result;
}

/* Todo add method
Expand Down Expand Up @@ -232,6 +291,12 @@ public static function getScopeByOfficeId(int $officeId): ThinnedScope|array

public static function getOfficeListByServiceId(int $serviceId, bool $showUnpublished = false): OfficeList|array
{
$cacheKey = self::CACHE_KEY_OFFICES_BY_SERVICE_PREFIX . $serviceId . ($showUnpublished ? '_unpublished' : '');

if (\App::$cache && ($cachedData = \App::$cache->get($cacheKey))) {
return $cachedData;
}

$providerList = ZmsApiClientService::getOffices() ?? new ProviderList();
$requestRelationList = ZmsApiClientService::getRequestRelationList() ?? new RequestRelationList();
$providerMap = [];
Expand Down Expand Up @@ -267,7 +332,11 @@ public static function getOfficeListByServiceId(int $serviceId, bool $showUnpubl
return $errors;
}

return new OfficeList($offices);
$result = new OfficeList($offices);

self::setMappedCache($cacheKey, $result);

return $result;
}

public static function getScopeById(?int $scopeId): ThinnedScope|array
Expand Down Expand Up @@ -320,6 +389,12 @@ public static function getScopeById(?int $scopeId): ThinnedScope|array

public static function getServicesByOfficeId(int $officeId, bool $showUnpublished = false): ServiceList|array
{
$cacheKey = self::CACHE_KEY_SERVICES_BY_OFFICE_PREFIX . $officeId . ($showUnpublished ? '_unpublished' : '');

if (\App::$cache && ($cachedData = \App::$cache->get($cacheKey))) {
return $cachedData;
}

$requestList = ZmsApiClientService::getServices() ?? new RequestList();
$requestRelationList = ZmsApiClientService::getRequestRelationList() ?? new RequestRelationList();
$requestMap = [];
Expand Down Expand Up @@ -352,7 +427,11 @@ public static function getServicesByOfficeId(int $officeId, bool $showUnpublishe
return $errors;
}

return new ServiceList($services);
$result = new ServiceList($services);

self::setMappedCache($cacheKey, $result);

return $result;
}

public static function getServicesProvidedAtOffice(int $officeId): RequestList|array
Expand Down
Loading