From 6ff10ad89e62a34f9edb5f292aedf9863f62c8e6 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Fri, 13 Sep 2024 12:24:21 +0200 Subject: [PATCH] Use flexible caching --- Blacklight/Books.php | 21 ++++++++++----------- Blacklight/Console.php | 24 +++++++++--------------- Blacklight/Games.php | 23 +++++++++-------------- Blacklight/Genres.php | 29 +++++++---------------------- Blacklight/Music.php | 18 ++++++++---------- Blacklight/XXX.php | 18 ++++++++---------- 6 files changed, 51 insertions(+), 82 deletions(-) diff --git a/Blacklight/Books.php b/Blacklight/Books.php index 259597ab9..c8be5a81f 100755 --- a/Blacklight/Books.php +++ b/Blacklight/Books.php @@ -102,7 +102,7 @@ class Books return BookInfo::search($searchWords)->first(); } - public function getBookRange($page, $cat, $start, $num, $orderBy, array $excludedCats = []): array + public function getBookRange($page, $cat, $start, $num, $orderBy, array $excludedCats = []): mixed { $browseby = $this->getBrowseBy(); $catsrch = ''; @@ -175,17 +175,16 @@ class Books $order[0], $order[1] ); - $return = Cache::get(md5($sql.$page)); - if ($return !== null) { - return $return; - } - $return = DB::select($sql); - if (\count($return) > 0) { - $return[0]->_totalcount = $books['total'][0]->total ?? 0; - } - Cache::put(md5($sql.$page), $return, $expiresAt); - return $return; + return Cache::flexible($sql.$page, [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($sql, $books) { + $return = DB::select($sql); + if (\count($return) > 0) { + $return[0]->_totalcount = $books['total'][0]->total ?? 0; + } + + return $return; + }); + } public function getBookOrder($orderBy): array diff --git a/Blacklight/Console.php b/Blacklight/Console.php index 67f744528..d07706f47 100755 --- a/Blacklight/Console.php +++ b/Blacklight/Console.php @@ -132,10 +132,7 @@ class Console return ConsoleInfo::search($searchWords)->first() ?? false; } - /** - * @throws \Exception - */ - public function getConsoleRange($page, $cat, $start, $num, $orderBy, array $excludedCats = []): array + public function getConsoleRange($page, $cat, $start, $num, $orderBy, array $excludedCats = []): mixed { $browseBy = $this->getBrowseBy(); $catsrch = ''; @@ -211,18 +208,15 @@ class Console $order[0], $order[1] ); - $return = Cache::get(md5($sql.$page)); - if ($return !== null) { - return $return; - } - $return = DB::select($sql); - if (\count($return) > 0) { - $return[0]->_totalcount = $consoles['total'][0]->total ?? 0; - } - $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); - Cache::put(md5($sql.$page), $return, $expiresAt); - return $return; + return Cache::flexible($sql.$page, [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($sql, $consoles) { + $return = DB::select($sql); + if (\count($return) > 0) { + $return[0]->_totalcount = $consoles['total'][0]->total ?? 0; + } + + return $return; + }); } public function getConsoleOrder($orderBy): array diff --git a/Blacklight/Games.php b/Blacklight/Games.php index 767300181..62b55def0 100755 --- a/Blacklight/Games.php +++ b/Blacklight/Games.php @@ -163,10 +163,7 @@ class Games return $res ?? 0; } - /** - * @throws \Exception - */ - public function getGamesRange($page, $cat, $start, $num, array|string $orderBy = '', string $maxAge = '', array $excludedCats = []): array + public function getGamesRange($page, $cat, $start, $num, array|string $orderBy = '', string $maxAge = '', array $excludedCats = []): mixed { $browseBy = $this->getBrowseBy(); $catsrch = ''; @@ -243,17 +240,15 @@ class Games $order[0], $order[1] ); - $return = Cache::get(md5($returnSql.$page)); - if ($return !== null) { - return $return; - } - $return = DB::select($returnSql); - if (\count($return) > 0) { - $return[0]->_totalcount = $games['total'][0]->total ?? 0; - } - Cache::put(md5($returnSql.$page), $return, $expiresAt); - return $return; + return Cache::flexible($returnSql.$page, [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($returnSql, $games) { + $return = DB::select($returnSql); + if (\count($return) > 0) { + $return[0]->_totalcount = $games['total'][0]->total ?? 0; + } + + return $return; + }); } public function getGamesOrder(array|string $orderBy): array diff --git a/Blacklight/Genres.php b/Blacklight/Genres.php index e33aac7e4..31a1dc7c2 100755 --- a/Blacklight/Genres.php +++ b/Blacklight/Genres.php @@ -25,21 +25,13 @@ class Genres public function __construct() {} - /** - * @return array|mixed - */ - public function getGenres(string $type = '', bool $activeOnly = false) + public function getGenres(string $type = '', bool $activeOnly = false): mixed { $sql = $this->getListQuery($type, $activeOnly); - $genres = Cache::get(md5($sql)); - if ($genres !== null) { - return $genres; - } - $genres = DB::select($sql); - $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); - Cache::put(md5($sql), $genres, $expiresAt); - return $genres; + return Cache::flexible($sql, [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($sql) { + return DB::select($sql); + }); } public function loadGenres($type): array @@ -156,15 +148,8 @@ class Genres */ public function getDisabledIDs() { - $cats = Cache::get('disabledcats'); - if ($cats !== null) { - $disabled = $cats; - } else { - $disabled = Genre::query()->where('disabled', '=', 1)->get(['id']); - $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); - Cache::put('disabledcats', $disabled, $expiresAt); - } - - return $disabled; + return Cache::flexible('disabledCats', [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () { + return Genre::query()->where('disabled', '=', 1)->get(['id']); + }); } } diff --git a/Blacklight/Music.php b/Blacklight/Music.php index fc455dfbf..6f0657d6b 100755 --- a/Blacklight/Music.php +++ b/Blacklight/Music.php @@ -201,17 +201,15 @@ class Music $order[0], $order[1] ); - $return = Cache::get(md5($sql.$page)); - if ($return !== null) { - return $return; - } - $return = MusicInfo::fromQuery($sql); - if ($return->isNotEmpty()) { - $return[0]->_totalcount = $music['total'][0]->total ?? 0; - } - Cache::put(md5($sql.$page), $return, $expiresAt); - return $return; + return Cache::flexible($sql.$page, [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($sql, $music) { + $return = DB::select($sql); + if (\count($return) > 0) { + $return[0]->_totalcount = $music['total'][0]->total ?? 0; + } + + return $return; + }); } public function getMusicOrder($orderBy): array diff --git a/Blacklight/XXX.php b/Blacklight/XXX.php index a54b3ec00..97c08010c 100755 --- a/Blacklight/XXX.php +++ b/Blacklight/XXX.php @@ -166,17 +166,15 @@ class XXX $order[0], $order[1] ); - $return = Cache::get(md5($sql.$page)); - if ($return !== null) { - return $return; - } - $return = DB::select($sql); - if (\count($return) > 0) { - $return[0]->_totalcount = $xxxmovies['total'][0]->total ?? 0; - } - Cache::put(md5($sql.$page), $return, $expiresAt); - return $return; + return Cache::flexible($sql.$page, [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($sql, $xxxmovies) { + $return = DB::select($sql); + if (\count($return) > 0) { + $return[0]->_totalcount = $xxxmovies['total'][0]->total ?? 0; + } + + return $return; + }); } /**