Микрооптимизация вычитки листов из кэша
This commit is contained in:
@@ -11,7 +11,6 @@ namespace App\Core;
|
|||||||
|
|
||||||
use App\Errors\PlaylistNotFoundException;
|
use App\Errors\PlaylistNotFoundException;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Throwable;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Класс для работы со списком плейлистов
|
* Класс для работы со списком плейлистов
|
||||||
@@ -31,27 +30,21 @@ 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) {
|
|
||||||
try {
|
|
||||||
$data = @redis()->get($code);
|
|
||||||
} catch (Throwable) {
|
|
||||||
$data = false;
|
|
||||||
}
|
|
||||||
if ($data === false) {
|
|
||||||
$raw = $ini[$code];
|
$raw = $ini[$code];
|
||||||
$data = [
|
$data === false && $data = [
|
||||||
'code' => $code,
|
'code' => $code,
|
||||||
'name' => $raw['name'] ?? "Playlist #$code",
|
'name' => $raw['name'] ?? "Плейлист #$code",
|
||||||
'description' => $raw['desc'] ?? null,
|
'description' => $raw['desc'] ?? null,
|
||||||
'url' => $raw['pls'],
|
'url' => $raw['pls'],
|
||||||
'source' => $raw['src'] ?? null,
|
'source' => $raw['src'] ?? null,
|
||||||
@@ -62,15 +55,17 @@ class IniFile
|
|||||||
'channels' => [],
|
'channels' => [],
|
||||||
'checkedAt' => null,
|
'checkedAt' => null,
|
||||||
];
|
];
|
||||||
} elseif (!isset($data['attributes'])) {
|
|
||||||
$data['attributes'] = [];
|
// приколы golang
|
||||||
}
|
$data['attributes'] === null && $data['attributes'] = [];
|
||||||
|
$data['groups'] === null && $data['groups'] = [];
|
||||||
|
$data['channels'] === null && $data['channels'] = [];
|
||||||
|
|
||||||
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user