Remove serialized DB field for AzuraRelay installs.

This commit is contained in:
Buster Neece 2023-05-31 23:46:22 -05:00
parent 8cfc889ea5
commit 6a6a3fb7d0
No known key found for this signature in database
5 changed files with 92 additions and 16 deletions

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Cache;
use App\Entity\Relay;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
final class AzuraRelayCache
{
private const CACHE_TTL = 600;
public function __construct(
private readonly CacheItemPoolInterface $cache
) {
}
public function setForRelay(
Relay $relay,
array $np
): void {
$cacheItem = $this->getCacheItem($relay);
$cacheItem->set($np);
$cacheItem->expiresAfter(self::CACHE_TTL);
$this->cache->save($cacheItem);
}
public function getForRelay(Relay $relay): array
{
$cacheItem = $this->getCacheItem($relay);
return $cacheItem->isHit()
? (array)$cacheItem->get()
: [];
}
private function getCacheItem(Relay $relay): CacheItemInterface
{
return $this->cache->getItem('azurarelay.relay_' . $relay->getIdRequired());
}
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller\Api\Admin;
use App\Cache\AzuraRelayCache;
use App\Entity;
use App\Enums\StationPermissions;
use App\Http\Response;
@ -39,7 +40,8 @@ final class RelaysController
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Adapters $adapters,
private readonly Entity\Repository\SettingsRepository $settingsRepo
private readonly Entity\Repository\SettingsRepository $settingsRepo,
private readonly AzuraRelayCache $azuraRelayCache
) {
}
@ -137,7 +139,6 @@ final class RelaysController
$relay->setName($body['name'] ?? 'Relay');
$relay->setIsVisibleOnPublicPages($body['is_visible_on_public_pages'] ?? true);
$relay->setNowplaying((array)$body['nowplaying']);
$relay->setUpdatedAt(time());
$this->em->persist($relay);
@ -189,6 +190,8 @@ final class RelaysController
$this->em->flush();
$this->azuraRelayCache->setForRelay($relay, (array)$body['nowplaying']);
return $response->withJson(Entity\Api\Status::success());
}
}

View File

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Entity\Migration;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20230601043650 extends AbstractMigration
{
public function getDescription(): string
{
return 'Remove serialized NowPlaying field for AzuraRelays.';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE relays DROP nowplaying');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE relays ADD nowplaying LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:array)\'');
}
}

View File

@ -40,9 +40,6 @@ class Relay implements IdentifiableEntityInterface
]
protected bool $is_visible_on_public_pages = true;
#[ORM\Column(type: 'array', nullable: true)]
protected mixed $nowplaying;
#[
OA\Property(example: OpenApi::SAMPLE_TIMESTAMP),
ORM\Column
@ -100,16 +97,6 @@ class Relay implements IdentifiableEntityInterface
$this->is_visible_on_public_pages = $is_visible_on_public_pages;
}
public function getNowplaying(): mixed
{
return $this->nowplaying;
}
public function setNowplaying(mixed $nowplaying): void
{
$this->nowplaying = $nowplaying;
}
public function getCreatedAt(): int
{
return $this->created_at;

View File

@ -4,16 +4,32 @@ declare(strict_types=1);
namespace App\Radio\Remote;
use App\Cache\AzuraRelayCache;
use App\Entity;
use App\Environment;
use Doctrine\ORM\EntityManagerInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Promise\Create;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Uri;
use InvalidArgumentException;
use Monolog\Logger;
use NowPlaying\AdapterFactory;
use NowPlaying\Result\Result;
final class AzuraRelay extends AbstractRemote
{
public function __construct(
EntityManagerInterface $em,
Entity\Repository\SettingsRepository $settingsRepo,
Client $http_client,
Logger $logger,
AdapterFactory $adapterFactory,
private readonly AzuraRelayCache $azuraRelayCache
) {
parent::__construct($em, $settingsRepo, $http_client, $logger, $adapterFactory);
}
public function getNowPlayingAsync(Entity\StationRemote $remote, bool $includeClients = false): PromiseInterface
{
$station = $remote->getStation();
@ -23,7 +39,7 @@ final class AzuraRelay extends AbstractRemote
throw new InvalidArgumentException('AzuraRelay remote must have a corresponding relay.');
}
$npRawRelay = $relay->getNowplaying();
$npRawRelay = $this->azuraRelayCache->getForRelay($relay);
if (isset($npRawRelay[$station->getId()][$remote->getMount()])) {
$npRaw = $npRawRelay[$station->getId()][$remote->getMount()];