Оптимизация вычитки листа из кэша
This commit is contained in:
@@ -35,13 +35,66 @@ class IniFile
|
|||||||
public function load(): void
|
public function load(): void
|
||||||
{
|
{
|
||||||
$filepath = config_path('playlists.ini');
|
$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));
|
$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) {
|
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 = [
|
$data === false && $data = [
|
||||||
'code' => $code,
|
'code' => $code,
|
||||||
'name' => $raw['name'] ?? "Плейлист #$code",
|
'name' => $raw['name'] ?? "Плейлист #$code",
|
||||||
@@ -55,6 +108,7 @@ class IniFile
|
|||||||
'channels' => [],
|
'channels' => [],
|
||||||
'checkedAt' => null,
|
'checkedAt' => null,
|
||||||
];
|
];
|
||||||
|
}
|
||||||
|
|
||||||
// приколы golang
|
// приколы golang
|
||||||
$data['attributes'] === null && $data['attributes'] = [];
|
$data['attributes'] === null && $data['attributes'] = [];
|
||||||
@@ -79,43 +133,6 @@ class IniFile
|
|||||||
$data['tags'] = array_values(array_unique($data['tags']));
|
$data['tags'] = array_values(array_unique($data['tags']));
|
||||||
sort($data['tags']);
|
sort($data['tags']);
|
||||||
|
|
||||||
$this->playlists[$code] = $data;
|
return $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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user