Changes for compatibility with newer Symfony Cache updates.
This commit is contained in:
parent
79c26179b3
commit
bb163c2142
|
@ -174,22 +174,22 @@ return [
|
||||||
Symfony\Component\Cache\Adapter\AdapterInterface::class => DI\get(
|
Symfony\Component\Cache\Adapter\AdapterInterface::class => DI\get(
|
||||||
Symfony\Contracts\Cache\CacheInterface::class
|
Symfony\Contracts\Cache\CacheInterface::class
|
||||||
),
|
),
|
||||||
|
|
||||||
Psr\Cache\CacheItemPoolInterface::class => DI\get(
|
Psr\Cache\CacheItemPoolInterface::class => DI\get(
|
||||||
Symfony\Contracts\Cache\CacheInterface::class
|
Symfony\Contracts\Cache\CacheInterface::class
|
||||||
),
|
),
|
||||||
Psr\SimpleCache\CacheInterface::class => static function (Psr\Cache\CacheItemPoolInterface $cache) {
|
|
||||||
return new Symfony\Component\Cache\Psr16Cache($cache);
|
Psr\SimpleCache\CacheInterface::class => static fn(
|
||||||
},
|
Psr\Cache\CacheItemPoolInterface $cache
|
||||||
|
) => new Symfony\Component\Cache\Psr16Cache($cache),
|
||||||
|
|
||||||
// Symfony Lock adapter
|
// Symfony Lock adapter
|
||||||
Symfony\Component\Lock\PersistingStoreInterface::class => static function (
|
Symfony\Component\Lock\PersistingStoreInterface::class => static fn(
|
||||||
Environment $environment,
|
Environment $environment,
|
||||||
App\Service\RedisFactory $redisFactory
|
App\Service\RedisFactory $redisFactory
|
||||||
) {
|
) => ($redisFactory->isSupported())
|
||||||
return ($redisFactory->isSupported())
|
|
||||||
? new Symfony\Component\Lock\Store\RedisStore($redisFactory->createInstance())
|
? new Symfony\Component\Lock\Store\RedisStore($redisFactory->createInstance())
|
||||||
: new Symfony\Component\Lock\Store\FlockStore($environment->getTempDirectory());
|
: new Symfony\Component\Lock\Store\FlockStore($environment->getTempDirectory()),
|
||||||
},
|
|
||||||
|
|
||||||
// Console
|
// Console
|
||||||
App\Console\Application::class => static function (
|
App\Console\Application::class => static function (
|
||||||
|
@ -235,6 +235,7 @@ return [
|
||||||
|
|
||||||
return $dispatcher;
|
return $dispatcher;
|
||||||
},
|
},
|
||||||
|
|
||||||
Psr\EventDispatcher\EventDispatcherInterface::class => DI\get(
|
Psr\EventDispatcher\EventDispatcherInterface::class => DI\get(
|
||||||
App\CallableEventDispatcherInterface::class
|
App\CallableEventDispatcherInterface::class
|
||||||
),
|
),
|
||||||
|
@ -259,6 +260,7 @@ return [
|
||||||
|
|
||||||
return $logger;
|
return $logger;
|
||||||
},
|
},
|
||||||
|
|
||||||
Psr\Log\LoggerInterface::class => DI\get(Monolog\Logger::class),
|
Psr\Log\LoggerInterface::class => DI\get(Monolog\Logger::class),
|
||||||
|
|
||||||
// Doctrine annotations reader
|
// Doctrine annotations reader
|
||||||
|
|
|
@ -12,9 +12,8 @@ use App\OpenApi;
|
||||||
use App\Radio\AutoDJ\Scheduler;
|
use App\Radio\AutoDJ\Scheduler;
|
||||||
use Carbon\CarbonImmutable;
|
use Carbon\CarbonImmutable;
|
||||||
use OpenApi\Attributes as OA;
|
use OpenApi\Attributes as OA;
|
||||||
|
use Psr\Cache\CacheItemPoolInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Symfony\Component\Cache\CacheItem;
|
|
||||||
use Symfony\Contracts\Cache\CacheInterface;
|
|
||||||
|
|
||||||
#[OA\Get(
|
#[OA\Get(
|
||||||
path: '/station/{station_id}/schedule',
|
path: '/station/{station_id}/schedule',
|
||||||
|
@ -57,9 +56,9 @@ final class ScheduleAction
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly Scheduler $scheduler,
|
private readonly Scheduler $scheduler,
|
||||||
private readonly CacheInterface $cache,
|
|
||||||
private readonly Entity\ApiGenerator\ScheduleApiGenerator $scheduleApiGenerator,
|
private readonly Entity\ApiGenerator\ScheduleApiGenerator $scheduleApiGenerator,
|
||||||
private readonly Entity\Repository\StationScheduleRepository $scheduleRepo
|
private readonly Entity\Repository\StationScheduleRepository $scheduleRepo,
|
||||||
|
private readonly CacheItemPoolInterface $psr6Cache
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,26 +79,27 @@ final class ScheduleAction
|
||||||
. $dateRange->getStart()->format('Ymd') . '-'
|
. $dateRange->getStart()->format('Ymd') . '-'
|
||||||
. $dateRange->getEnd()->format('Ymd');
|
. $dateRange->getEnd()->format('Ymd');
|
||||||
|
|
||||||
$events = $this->cache->get(
|
$cacheItem = $this->psr6Cache->getItem($cacheKey);
|
||||||
$cacheKey,
|
|
||||||
function (CacheItem $item) use (
|
|
||||||
$station,
|
|
||||||
$dateRange
|
|
||||||
) {
|
|
||||||
$item->expiresAfter(600);
|
|
||||||
|
|
||||||
|
if (!$cacheItem->isHit()) {
|
||||||
$nowTz = CarbonImmutable::now($station->getTimezoneObject());
|
$nowTz = CarbonImmutable::now($station->getTimezoneObject());
|
||||||
$events = $this->scheduleRepo->getAllScheduledItemsForStation($station);
|
$events = $this->scheduleRepo->getAllScheduledItemsForStation($station);
|
||||||
|
|
||||||
return $this->getEvents(
|
$cacheItem->set(
|
||||||
|
$this->getEvents(
|
||||||
$dateRange,
|
$dateRange,
|
||||||
$nowTz,
|
$nowTz,
|
||||||
$this->scheduler,
|
$this->scheduler,
|
||||||
$events,
|
$events,
|
||||||
[$this->scheduleApiGenerator, '__invoke']
|
[$this->scheduleApiGenerator, '__invoke']
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
$cacheItem->expiresAfter(600);
|
||||||
|
|
||||||
|
$this->psr6Cache->saveDeferred($cacheItem);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
$events = $cacheItem->get();
|
||||||
} else {
|
} else {
|
||||||
if (!empty($queryParams['now'])) {
|
if (!empty($queryParams['now'])) {
|
||||||
$now = CarbonImmutable::parse($queryParams['now'], $tz);
|
$now = CarbonImmutable::parse($queryParams['now'], $tz);
|
||||||
|
@ -109,13 +109,16 @@ final class ScheduleAction
|
||||||
$cacheKey = 'api_station_' . $station->getId() . '_schedule_upcoming';
|
$cacheKey = 'api_station_' . $station->getId() . '_schedule_upcoming';
|
||||||
}
|
}
|
||||||
|
|
||||||
$events = $this->cache->get(
|
$cacheItem = $this->psr6Cache->getItem($cacheKey);
|
||||||
$cacheKey,
|
|
||||||
function (CacheItem $item) use ($station, $now) {
|
if (!$cacheItem->isHit()) {
|
||||||
$item->expiresAfter(60);
|
$cacheItem->set($this->scheduleRepo->getUpcomingSchedule($station, $now));
|
||||||
return $this->scheduleRepo->getUpcomingSchedule($station, $now);
|
$cacheItem->expiresAfter(60);
|
||||||
|
|
||||||
|
$this->psr6Cache->saveDeferred($cacheItem);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
$events = $cacheItem->get();
|
||||||
|
|
||||||
$rows = (int)$request->getQueryParam('rows', 5);
|
$rows = (int)$request->getQueryParam('rows', 5);
|
||||||
$events = array_slice($events, 0, $rows);
|
$events = array_slice($events, 0, $rows);
|
||||||
|
|
|
@ -7,12 +7,10 @@ namespace App\Service;
|
||||||
use App\Service\DeviceDetector\DeviceResult;
|
use App\Service\DeviceDetector\DeviceResult;
|
||||||
use Psr\Cache\CacheItemPoolInterface;
|
use Psr\Cache\CacheItemPoolInterface;
|
||||||
use Symfony\Component\Cache\Adapter\ProxyAdapter;
|
use Symfony\Component\Cache\Adapter\ProxyAdapter;
|
||||||
use Symfony\Component\Cache\CacheItem;
|
|
||||||
use Symfony\Contracts\Cache\CacheInterface;
|
|
||||||
|
|
||||||
final class DeviceDetector
|
final class DeviceDetector
|
||||||
{
|
{
|
||||||
private CacheInterface $cache;
|
private CacheItemPoolInterface $psr6Cache;
|
||||||
|
|
||||||
private \DeviceDetector\DeviceDetector $dd;
|
private \DeviceDetector\DeviceDetector $dd;
|
||||||
|
|
||||||
|
@ -22,7 +20,7 @@ final class DeviceDetector
|
||||||
public function __construct(
|
public function __construct(
|
||||||
CacheItemPoolInterface $psr6Cache
|
CacheItemPoolInterface $psr6Cache
|
||||||
) {
|
) {
|
||||||
$this->cache = new ProxyAdapter($psr6Cache, 'device_detector.');
|
$this->psr6Cache = new ProxyAdapter($psr6Cache, 'device_detector.');
|
||||||
|
|
||||||
$this->dd = new \DeviceDetector\DeviceDetector();
|
$this->dd = new \DeviceDetector\DeviceDetector();
|
||||||
}
|
}
|
||||||
|
@ -35,19 +33,21 @@ final class DeviceDetector
|
||||||
return $this->deviceResults[$userAgentHash];
|
return $this->deviceResults[$userAgentHash];
|
||||||
}
|
}
|
||||||
|
|
||||||
$deviceResult = $this->cache->get(
|
$cacheItem = $this->psr6Cache->getItem($userAgentHash);
|
||||||
$userAgentHash,
|
|
||||||
function (CacheItem $item) use ($userAgent) {
|
|
||||||
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
|
|
||||||
$item->expiresAfter(86400 * 7);
|
|
||||||
|
|
||||||
|
if (!$cacheItem->isHit()) {
|
||||||
$this->dd->setUserAgent($userAgent);
|
$this->dd->setUserAgent($userAgent);
|
||||||
$this->dd->parse();
|
$this->dd->parse();
|
||||||
|
|
||||||
return DeviceResult::fromDeviceDetector($userAgent, $this->dd);
|
$cacheItem->set(DeviceResult::fromDeviceDetector($userAgent, $this->dd));
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
|
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
|
||||||
|
$cacheItem->expiresAfter(86400 * 7);
|
||||||
|
|
||||||
|
$this->psr6Cache->saveDeferred($cacheItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
$deviceResult = $cacheItem->get();
|
||||||
$this->deviceResults[$userAgentHash] = $deviceResult;
|
$this->deviceResults[$userAgentHash] = $deviceResult;
|
||||||
|
|
||||||
return $deviceResult;
|
return $deviceResult;
|
||||||
|
|
|
@ -10,8 +10,6 @@ use MaxMind\Db\Reader;
|
||||||
use Psr\Cache\CacheItemPoolInterface;
|
use Psr\Cache\CacheItemPoolInterface;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use Symfony\Component\Cache\Adapter\ProxyAdapter;
|
use Symfony\Component\Cache\Adapter\ProxyAdapter;
|
||||||
use Symfony\Component\Cache\CacheItem;
|
|
||||||
use Symfony\Contracts\Cache\CacheInterface;
|
|
||||||
|
|
||||||
final class IpGeolocation
|
final class IpGeolocation
|
||||||
{
|
{
|
||||||
|
@ -23,11 +21,11 @@ final class IpGeolocation
|
||||||
|
|
||||||
private string $attribution = '';
|
private string $attribution = '';
|
||||||
|
|
||||||
private CacheInterface $cache;
|
private CacheItemPoolInterface $psr6Cache;
|
||||||
|
|
||||||
public function __construct(CacheItemPoolInterface $psr6Cache)
|
public function __construct(CacheItemPoolInterface $psr6Cache)
|
||||||
{
|
{
|
||||||
$this->cache = new ProxyAdapter($psr6Cache, 'ip_geo.');
|
$this->psr6Cache = new ProxyAdapter($psr6Cache, 'ip_geo.');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function initialize(): void
|
private function initialize(): void
|
||||||
|
@ -82,30 +80,33 @@ final class IpGeolocation
|
||||||
|
|
||||||
$cacheKey = $this->readerShortName . '_' . str_replace([':', '.'], '_', $ip);
|
$cacheKey = $this->readerShortName . '_' . str_replace([':', '.'], '_', $ip);
|
||||||
|
|
||||||
$ipInfo = $this->cache->get(
|
$cacheItem = $this->psr6Cache->getItem($cacheKey);
|
||||||
$cacheKey,
|
|
||||||
function (CacheItem $item) use ($ip, $reader) {
|
|
||||||
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
|
|
||||||
$item->expiresAfter(86400 * 7);
|
|
||||||
|
|
||||||
|
if (!$cacheItem->isHit()) {
|
||||||
try {
|
try {
|
||||||
$ipInfo = $reader->get($ip);
|
$ipInfo = $reader->get($ip);
|
||||||
if (!empty($ipInfo)) {
|
if (!empty($ipInfo)) {
|
||||||
return $ipInfo;
|
return $ipInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
$cacheItem->set([
|
||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'message' => 'Internal/Reserved IP',
|
'message' => 'Internal/Reserved IP',
|
||||||
];
|
]);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
return [
|
$cacheItem->set([
|
||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'message' => $e->getMessage(),
|
'message' => $e->getMessage(),
|
||||||
];
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
|
||||||
|
$cacheItem->expiresAfter(86400 * 7);
|
||||||
|
|
||||||
|
$this->psr6Cache->saveDeferred($cacheItem);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
$ipInfo = $cacheItem->get();
|
||||||
|
|
||||||
return IpGeolocator\IpResult::fromIpInfo($ip, $ipInfo);
|
return IpGeolocator\IpResult::fromIpInfo($ip, $ipInfo);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue