CaiWuXiTong/modules/EcommerceFinance/Services/UpstreamReadOnlyQueryFactory.php

33 lines
971 B
PHP

<?php
namespace Modules\EcommerceFinance\Services;
class UpstreamReadOnlyQueryFactory
{
/** @var array<string,UpstreamReadOnlyQuery> */
private array $instances = [];
/** @var array<string,UpstreamReadOnlyQuery> */
private array $timedInstances = [];
public function __construct(private readonly UpstreamSourceRegistry $sources)
{
}
public function forSource(string $source): UpstreamReadOnlyQuery
{
$key = $this->sources->definition($source)['key'];
return $this->instances[$key] ??= new UpstreamReadOnlyQuery($key);
}
public function forSourceWithTimeout(string $source, int $timeoutSeconds): UpstreamReadOnlyQuery
{
$key = $this->sources->definition($source)['key'];
$timeoutSeconds = max(1, min($timeoutSeconds, 120));
$cacheKey = $key . '@' . $timeoutSeconds;
return $this->timedInstances[$cacheKey] ??= new UpstreamReadOnlyQuery($key, $timeoutSeconds);
}
}