Оптимизация вычитки листа из кэша
This commit is contained in:
@@ -35,13 +35,66 @@ class IniFile
|
||||
public function load(): void
|
||||
{
|
||||
$filepath = config_path('playlists.ini');
|
||||
$ini = parse_ini_file($filepath, true);
|
||||
$this->playlists = parse_ini_file($filepath, true);
|
||||
$this->updatedAt = date('d.m.Y h:i', filemtime($filepath));
|
||||
$plsCodes = array_keys($ini);
|
||||
$cached = array_combine($plsCodes, redis()->mget($plsCodes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает плейлисты
|
||||
*
|
||||
* @return array[]
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPlaylists(): array
|
||||
{
|
||||
empty($this->playlists) && $this->load();
|
||||
$plsCodes = array_keys($this->playlists);
|
||||
$cached = array_combine($plsCodes, redis()->mget($plsCodes));
|
||||
foreach ($cached as $code => $data) {
|
||||
$raw = $ini[$code];
|
||||
$this->playlists[$code] = $this->initPlaylist($code, $data);
|
||||
}
|
||||
|
||||
return $this->playlists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает плейлист по его коду
|
||||
*
|
||||
* @param string $code Код плейлиста
|
||||
* @return array|null
|
||||
* @throws PlaylistNotFoundException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPlaylist(string $code): ?array
|
||||
{
|
||||
empty($this->playlists) && $this->load();
|
||||
$data = redis()->get($code);
|
||||
$pls = $this->initPlaylist($code, $data);
|
||||
|
||||
return $pls ?? throw new PlaylistNotFoundException($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает дату обновления ini-файла
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function updatedAt(): string
|
||||
{
|
||||
return $this->updatedAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Подготавливает данные о плейлисте в расширенном формате
|
||||
*
|
||||
* @param string $code
|
||||
* @param array|false $data
|
||||
* @return array
|
||||
*/
|
||||
protected function initPlaylist(string $code, array|false $data): array
|
||||
{
|
||||
if ($data === false) {
|
||||
$raw = $this->playlists[$code];
|
||||
$data === false && $data = [
|
||||
'code' => $code,
|
||||
'name' => $raw['name'] ?? "Плейлист #$code",
|
||||
@@ -55,6 +108,7 @@ class IniFile
|
||||
'channels' => [],
|
||||
'checkedAt' => null,
|
||||
];
|
||||
}
|
||||
|
||||
// приколы golang
|
||||
$data['attributes'] === null && $data['attributes'] = [];
|
||||
@@ -79,43 +133,6 @@ class IniFile
|
||||
$data['tags'] = array_values(array_unique($data['tags']));
|
||||
sort($data['tags']);
|
||||
|
||||
$this->playlists[$code] = $data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает плейлисты
|
||||
*
|
||||
* @return array[]
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPlaylists(): array
|
||||
{
|
||||
empty($this->playlists) && $this->load();
|
||||
return $this->playlists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает плейлист по его коду
|
||||
*
|
||||
* @param string $code Код плейлиста
|
||||
* @return array|null
|
||||
* @throws PlaylistNotFoundException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPlaylist(string $code): ?array
|
||||
{
|
||||
empty($this->playlists) && $this->load();
|
||||
return $this->playlists[$code] ?? throw new PlaylistNotFoundException($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает дату обновления ini-файла
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function updatedAt(): string
|
||||
{
|
||||
return $this->updatedAt;
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user