Оптимизация вычитки листов из кэша с пагинацией, удалены количества онлайн/офлайн листов

This commit is contained in:
2025-10-22 00:59:56 +08:00
parent 71304f6d84
commit 993625aa8f
3 changed files with 18 additions and 24 deletions

View File

@@ -36,28 +36,24 @@ class WebController extends BasicController
*/ */
public function home(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface public function home(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{ {
$playlists = ini()->getPlaylists(); $ini = ini()->load();
$count = count($playlists);
$onlineCount = count(array_filter($playlists, static fn (array $playlist) => $playlist['isOnline'] === true));
$uncheckedCount = count(array_filter($playlists, static fn (array $playlist) => $playlist['isOnline'] === null));
$offlineCount = $count - $onlineCount - $uncheckedCount;
$count = count($ini);
$pageSize = config('app.page_size'); $pageSize = config('app.page_size');
if ($pageSize > 0) { if ($pageSize > 0) {
$pageCurrent = (int)($request->getAttributes()['page'] ?? $request->getQueryParams()['page'] ?? 1); $pageCurrent = (int)($request->getAttributes()['page'] ?? $request->getQueryParams()['page'] ?? 1);
$pageCount = ceil($count / $pageSize); $pageCount = ceil($count / $pageSize);
$offset = max(0, ($pageCurrent - 1) * $pageSize); $offset = max(0, ($pageCurrent - 1) * $pageSize);
$playlists = array_slice($playlists, $offset, $pageSize, true); $ini = array_slice($ini, $offset, $pageSize, true);
$playlists = ini()->getPlaylists(array_keys($ini));
} else {
$playlists = ini()->getPlaylists();
} }
return $this->view($request, $response, 'list.twig', [ return $this->view($request, $response, 'list.twig', [
'updatedAt' => ini()->updatedAt(), 'updatedAt' => ini()->updatedAt(),
'playlists' => $playlists, 'playlists' => $playlists,
'count' => $count, 'count' => $count,
'onlineCount' => $onlineCount,
'uncheckedCount' => $uncheckedCount,
'offlineCount' => $offlineCount,
'pageCount' => $pageCount ?? 1, 'pageCount' => $pageCount ?? 1,
'pageCurrent' => $pageCurrent ?? 1, 'pageCurrent' => $pageCurrent ?? 1,
]); ]);

View File

@@ -30,13 +30,15 @@ class IniFile
/** /**
* Считывает ini-файл и инициализирует плейлисты * Считывает ini-файл и инициализирует плейлисты
* *
* @return void * @return array
*/ */
public function load(): void public function load(): array
{ {
$filepath = config_path('playlists.ini'); $filepath = config_path('playlists.ini');
$this->playlists = 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));
return $this->playlists;
} }
/** /**
@@ -45,16 +47,17 @@ class IniFile
* @return array[] * @return array[]
* @throws Exception * @throws Exception
*/ */
public function getPlaylists(): array public function getPlaylists(array $plsCodes = []): array
{ {
$playlists = [];
empty($this->playlists) && $this->load(); empty($this->playlists) && $this->load();
$plsCodes = array_keys($this->playlists); empty($plsCodes) && $plsCodes = array_keys($this->playlists);
$cached = array_combine($plsCodes, redis()->mget($plsCodes)); $cached = array_combine($plsCodes, redis()->mget($plsCodes));
foreach ($cached as $code => $data) { foreach ($cached as $code => $data) {
$this->playlists[$code] = $this->initPlaylist($code, $data); $playlists[$code] = $this->initPlaylist($code, $data);
} }
return $this->playlists; return $playlists;
} }
/** /**
@@ -69,9 +72,7 @@ class IniFile
{ {
empty($this->playlists) && $this->load(); empty($this->playlists) && $this->load();
$data = redis()->get($code); $data = redis()->get($code);
$pls = $this->initPlaylist($code, $data); return $this->initPlaylist($code, $data);
return $pls ?? throw new PlaylistNotFoundException($code);
} }
/** /**
@@ -90,11 +91,13 @@ class IniFile
* @param string $code * @param string $code
* @param array|false $data * @param array|false $data
* @return array * @return array
* @throws PlaylistNotFoundException
*/ */
protected function initPlaylist(string $code, array|false $data): array protected function initPlaylist(string $code, array|false $data): array
{ {
if ($data === false) { if ($data === false) {
$raw = $this->playlists[$code]; $raw = $this->playlists[$code];
empty($raw) && throw new PlaylistNotFoundException($code);
$data === false && $data = [ $data === false && $data = [
'code' => $code, 'code' => $code,
'name' => $raw['name'] ?? "Плейлист #$code", 'name' => $raw['name'] ?? "Плейлист #$code",

View File

@@ -31,11 +31,6 @@
<h2 class="mb-0">Список плейлистов ({{ count }})</h2> <h2 class="mb-0">Список плейлистов ({{ count }})</h2>
<div class="text-muted small">Изменён {{ updatedAt }} МСК</div> <div class="text-muted small">Изменён {{ updatedAt }} МСК</div>
</div> </div>
<div class="d-flex flex-wrap gap-2 mb-2">
<span class="badge bg-success">online: {{ onlineCount }}</span>
<span class="badge bg-danger">offline: {{ offlineCount }}</span>
<span class="badge bg-secondary">unknown: {{ uncheckedCount }}</span>
</div>
</div> </div>
{% endblock %} {% endblock %}