From bb163c2142cc8944235a2f7b3d6043a0c347fa6a Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Thu, 2 Mar 2023 08:33:57 -0600 Subject: [PATCH] Changes for compatibility with newer Symfony Cache updates. --- config/services.php | 20 ++++---- .../Api/Stations/ScheduleAction.php | 51 ++++++++++--------- src/Service/DeviceDetector.php | 28 +++++----- src/Service/IpGeolocation.php | 51 ++++++++++--------- 4 files changed, 78 insertions(+), 72 deletions(-) diff --git a/config/services.php b/config/services.php index 1d4bb5b55..eb86bc72c 100644 --- a/config/services.php +++ b/config/services.php @@ -174,22 +174,22 @@ return [ Symfony\Component\Cache\Adapter\AdapterInterface::class => DI\get( Symfony\Contracts\Cache\CacheInterface::class ), + Psr\Cache\CacheItemPoolInterface::class => DI\get( 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\Component\Lock\PersistingStoreInterface::class => static function ( + Symfony\Component\Lock\PersistingStoreInterface::class => static fn( Environment $environment, App\Service\RedisFactory $redisFactory - ) { - return ($redisFactory->isSupported()) - ? new Symfony\Component\Lock\Store\RedisStore($redisFactory->createInstance()) - : new Symfony\Component\Lock\Store\FlockStore($environment->getTempDirectory()); - }, + ) => ($redisFactory->isSupported()) + ? new Symfony\Component\Lock\Store\RedisStore($redisFactory->createInstance()) + : new Symfony\Component\Lock\Store\FlockStore($environment->getTempDirectory()), // Console App\Console\Application::class => static function ( @@ -235,6 +235,7 @@ return [ return $dispatcher; }, + Psr\EventDispatcher\EventDispatcherInterface::class => DI\get( App\CallableEventDispatcherInterface::class ), @@ -259,6 +260,7 @@ return [ return $logger; }, + Psr\Log\LoggerInterface::class => DI\get(Monolog\Logger::class), // Doctrine annotations reader diff --git a/src/Controller/Api/Stations/ScheduleAction.php b/src/Controller/Api/Stations/ScheduleAction.php index 4be460646..d75675135 100644 --- a/src/Controller/Api/Stations/ScheduleAction.php +++ b/src/Controller/Api/Stations/ScheduleAction.php @@ -12,9 +12,8 @@ use App\OpenApi; use App\Radio\AutoDJ\Scheduler; use Carbon\CarbonImmutable; use OpenApi\Attributes as OA; +use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\ResponseInterface; -use Symfony\Component\Cache\CacheItem; -use Symfony\Contracts\Cache\CacheInterface; #[OA\Get( path: '/station/{station_id}/schedule', @@ -57,9 +56,9 @@ final class ScheduleAction public function __construct( private readonly Scheduler $scheduler, - private readonly CacheInterface $cache, 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->getEnd()->format('Ymd'); - $events = $this->cache->get( - $cacheKey, - function (CacheItem $item) use ( - $station, - $dateRange - ) { - $item->expiresAfter(600); + $cacheItem = $this->psr6Cache->getItem($cacheKey); - $nowTz = CarbonImmutable::now($station->getTimezoneObject()); - $events = $this->scheduleRepo->getAllScheduledItemsForStation($station); + if (!$cacheItem->isHit()) { + $nowTz = CarbonImmutable::now($station->getTimezoneObject()); + $events = $this->scheduleRepo->getAllScheduledItemsForStation($station); - return $this->getEvents( + $cacheItem->set( + $this->getEvents( $dateRange, $nowTz, $this->scheduler, $events, [$this->scheduleApiGenerator, '__invoke'] - ); - } - ); + ) + ); + $cacheItem->expiresAfter(600); + + $this->psr6Cache->saveDeferred($cacheItem); + } + + $events = $cacheItem->get(); } else { if (!empty($queryParams['now'])) { $now = CarbonImmutable::parse($queryParams['now'], $tz); @@ -109,13 +109,16 @@ final class ScheduleAction $cacheKey = 'api_station_' . $station->getId() . '_schedule_upcoming'; } - $events = $this->cache->get( - $cacheKey, - function (CacheItem $item) use ($station, $now) { - $item->expiresAfter(60); - return $this->scheduleRepo->getUpcomingSchedule($station, $now); - } - ); + $cacheItem = $this->psr6Cache->getItem($cacheKey); + + if (!$cacheItem->isHit()) { + $cacheItem->set($this->scheduleRepo->getUpcomingSchedule($station, $now)); + $cacheItem->expiresAfter(60); + + $this->psr6Cache->saveDeferred($cacheItem); + } + + $events = $cacheItem->get(); $rows = (int)$request->getQueryParam('rows', 5); $events = array_slice($events, 0, $rows); diff --git a/src/Service/DeviceDetector.php b/src/Service/DeviceDetector.php index 435bfb499..72e392af9 100644 --- a/src/Service/DeviceDetector.php +++ b/src/Service/DeviceDetector.php @@ -7,12 +7,10 @@ namespace App\Service; use App\Service\DeviceDetector\DeviceResult; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Cache\Adapter\ProxyAdapter; -use Symfony\Component\Cache\CacheItem; -use Symfony\Contracts\Cache\CacheInterface; final class DeviceDetector { - private CacheInterface $cache; + private CacheItemPoolInterface $psr6Cache; private \DeviceDetector\DeviceDetector $dd; @@ -22,7 +20,7 @@ final class DeviceDetector public function __construct( CacheItemPoolInterface $psr6Cache ) { - $this->cache = new ProxyAdapter($psr6Cache, 'device_detector.'); + $this->psr6Cache = new ProxyAdapter($psr6Cache, 'device_detector.'); $this->dd = new \DeviceDetector\DeviceDetector(); } @@ -35,19 +33,21 @@ final class DeviceDetector return $this->deviceResults[$userAgentHash]; } - $deviceResult = $this->cache->get( - $userAgentHash, - function (CacheItem $item) use ($userAgent) { - /** @noinspection SummerTimeUnsafeTimeManipulationInspection */ - $item->expiresAfter(86400 * 7); + $cacheItem = $this->psr6Cache->getItem($userAgentHash); - $this->dd->setUserAgent($userAgent); - $this->dd->parse(); + if (!$cacheItem->isHit()) { + $this->dd->setUserAgent($userAgent); + $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; return $deviceResult; diff --git a/src/Service/IpGeolocation.php b/src/Service/IpGeolocation.php index 162dee9ea..1bb01ff68 100644 --- a/src/Service/IpGeolocation.php +++ b/src/Service/IpGeolocation.php @@ -10,8 +10,6 @@ use MaxMind\Db\Reader; use Psr\Cache\CacheItemPoolInterface; use RuntimeException; use Symfony\Component\Cache\Adapter\ProxyAdapter; -use Symfony\Component\Cache\CacheItem; -use Symfony\Contracts\Cache\CacheInterface; final class IpGeolocation { @@ -23,11 +21,11 @@ final class IpGeolocation private string $attribution = ''; - private CacheInterface $cache; + private 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 @@ -82,30 +80,33 @@ final class IpGeolocation $cacheKey = $this->readerShortName . '_' . str_replace([':', '.'], '_', $ip); - $ipInfo = $this->cache->get( - $cacheKey, - function (CacheItem $item) use ($ip, $reader) { - /** @noinspection SummerTimeUnsafeTimeManipulationInspection */ - $item->expiresAfter(86400 * 7); + $cacheItem = $this->psr6Cache->getItem($cacheKey); - try { - $ipInfo = $reader->get($ip); - if (!empty($ipInfo)) { - return $ipInfo; - } - - return [ - 'status' => 'error', - 'message' => 'Internal/Reserved IP', - ]; - } catch (Exception $e) { - return [ - 'status' => 'error', - 'message' => $e->getMessage(), - ]; + if (!$cacheItem->isHit()) { + try { + $ipInfo = $reader->get($ip); + if (!empty($ipInfo)) { + return $ipInfo; } + + $cacheItem->set([ + 'status' => 'error', + 'message' => 'Internal/Reserved IP', + ]); + } catch (Exception $e) { + $cacheItem->set([ + 'status' => 'error', + 'message' => $e->getMessage(), + ]); } - ); + + /** @noinspection SummerTimeUnsafeTimeManipulationInspection */ + $cacheItem->expiresAfter(86400 * 7); + + $this->psr6Cache->saveDeferred($cacheItem); + } + + $ipInfo = $cacheItem->get(); return IpGeolocator\IpResult::fromIpInfo($ip, $ipInfo); }