Skip to content

Commit

Permalink
Revert "Revert "Use flexible caching and improve code""
Browse files Browse the repository at this point in the history
This reverts commit 6a5b1fc.
  • Loading branch information
DariusIII committed Sep 13, 2024
1 parent 72e57c0 commit 059b281
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions Blacklight/processing/Videos.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,33 +300,25 @@ public function addAliases($videoId, array $aliases = []): void
}
}

/**
* Retrieves all aliases for given VideoID or VideoID for a given alias.
*
*
* @return VideoAlias[]|bool|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|mixed
*/
public function getAliases(int $videoId, string $alias = ''): mixed
{
$return = false;
$expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
if ($videoId <= 0 && $alias === '') {
return false;
}

if ($videoId > 0 || $alias !== '') {
$aliasCache = Cache::get(md5($videoId.$alias));
if ($aliasCache !== null) {
$return = $aliasCache;
} else {
$sql = VideoAlias::query();
if ($videoId > 0) {
$sql->where('videos_id', $videoId);
} elseif ($alias !== '') {
$sql->where('title', $alias);
}
$return = $sql->get();
Cache::put(md5($videoId.$alias), $return, $expiresAt);
}
$cacheKey = md5($videoId.$alias);

$sql = VideoAlias::query();
if ($videoId > 0) {
$sql->where('videos_id', $videoId);
} else {
$sql->where('title', $alias);
}

return $return->isEmpty() ? false : $return;
return Cache::flexible($cacheKey, [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($sql) {
$result = $sql->get();

return $result->isEmpty() ? false : $result;
});
}
}

0 comments on commit 059b281

Please sign in to comment.