Changes for compatibility with newer Symfony Cache updates.

This commit is contained in:
Buster Neece 2023-03-02 08:33:57 -06:00
parent 79c26179b3
commit bb163c2142
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
4 changed files with 78 additions and 72 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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);
}