Микрооптимизация вычитки листов из кэша

This commit is contained in:
2025-10-21 23:58:13 +08:00
parent 65c9250c41
commit 1b601b39bf

View File

@@ -11,7 +11,6 @@ namespace App\Core;
use App\Errors\PlaylistNotFoundException; use App\Errors\PlaylistNotFoundException;
use Exception; use Exception;
use Throwable;
/** /**
* Класс для работы со списком плейлистов * Класс для работы со списком плейлистов
@@ -31,46 +30,42 @@ class IniFile
/** /**
* Считывает ini-файл и инициализирует плейлисты * Считывает ini-файл и инициализирует плейлисты
* *
* @return array * @return void
* @throws Exception
*/ */
public function load(): array public function load(): void
{ {
$filepath = config_path('playlists.ini'); $filepath = config_path('playlists.ini');
$ini = parse_ini_file($filepath, true); $ini = parse_ini_file($filepath, true);
$this->updatedAt = date('d.m.Y h:i', filemtime($filepath)); $this->updatedAt = date('d.m.Y h:i', filemtime($filepath));
$plsCodes = array_keys($ini);
$cached = array_combine($plsCodes, redis()->mget($plsCodes));
// сохраняем порядок foreach ($cached as $code => $data) {
foreach (array_keys($ini) as $code) { $raw = $ini[$code];
try { $data === false && $data = [
$data = @redis()->get($code); 'code' => $code,
} catch (Throwable) { 'name' => $raw['name'] ?? "Плейлист #$code",
$data = false; 'description' => $raw['desc'] ?? null,
} 'url' => $raw['pls'],
if ($data === false) { 'source' => $raw['src'] ?? null,
$raw = $ini[$code]; 'content' => null,
$data = [ 'isOnline' => null,
'code' => $code, 'attributes' => [],
'name' => $raw['name'] ?? "Playlist #$code", 'groups' => [],
'description' => $raw['desc'] ?? null, 'channels' => [],
'url' => $raw['pls'], 'checkedAt' => null,
'source' => $raw['src'] ?? null, ];
'content' => null,
'isOnline' => null, // приколы golang
'attributes' => [], $data['attributes'] === null && $data['attributes'] = [];
'groups' => [], $data['groups'] === null && $data['groups'] = [];
'channels' => [], $data['channels'] === null && $data['channels'] = [];
'checkedAt' => null,
];
} elseif (!isset($data['attributes'])) {
$data['attributes'] = [];
}
$data['onlinePercent'] = 0; $data['onlinePercent'] = 0;
$data['offlinePercent'] = 0; $data['offlinePercent'] = 0;
if ($data['isOnline'] === true && count($data['channels'] ?? []) > 0) { if ($data['isOnline'] === true && count($data['channels']) > 0) {
$data['onlinePercent'] = round($data['onlineCount'] / count($data['channels'] ?? []) * 100); $data['onlinePercent'] = round($data['onlineCount'] / count($data['channels']) * 100);
$data['offlinePercent'] = round($data['offlineCount'] / count($data['channels'] ?? []) * 100); $data['offlinePercent'] = round($data['offlineCount'] / count($data['channels']) * 100);
} }
$data['hasCatchup'] = str_contains($data['content'] ?? '', 'catchup'); $data['hasCatchup'] = str_contains($data['content'] ?? '', 'catchup');
@@ -78,7 +73,7 @@ class IniFile
|| !empty($data['attributes']['x-tvg-url']); || !empty($data['attributes']['x-tvg-url']);
$data['tags'] = []; $data['tags'] = [];
foreach ($data['channels'] ?? [] as $channel) { foreach ($data['channels'] as $channel) {
$data['tags'] = array_merge($data['tags'], $channel['tags']); $data['tags'] = array_merge($data['tags'], $channel['tags']);
} }
$data['tags'] = array_values(array_unique($data['tags'])); $data['tags'] = array_values(array_unique($data['tags']));
@@ -86,8 +81,6 @@ class IniFile
$this->playlists[$code] = $data; $this->playlists[$code] = $data;
} }
return $this->playlists;
} }
/** /**
@@ -98,17 +91,8 @@ class IniFile
*/ */
public function getPlaylists(): array public function getPlaylists(): array
{ {
return $this->playlists ??= $this->load(); empty($this->playlists) && $this->load();
} return $this->playlists;
/**
* Возвращает дату обновления ini-файла
*
* @return string
*/
public function updatedAt(): string
{
return $this->updatedAt;
} }
/** /**
@@ -121,10 +105,17 @@ class IniFile
*/ */
public function getPlaylist(string $code): ?array public function getPlaylist(string $code): ?array
{ {
if (empty($this->playlists)) { empty($this->playlists) && $this->load();
$this->load();
}
return $this->playlists[$code] ?? throw new PlaylistNotFoundException($code); return $this->playlists[$code] ?? throw new PlaylistNotFoundException($code);
} }
/**
* Возвращает дату обновления ini-файла
*
* @return string
*/
public function updatedAt(): string
{
return $this->updatedAt;
}
} }