2017-01-06 03:04:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2017-04-28 14:36:57 +00:00
|
|
|
use App\Models\Album;
|
2017-01-06 03:04:08 +00:00
|
|
|
use App\Models\Artist;
|
2017-04-28 14:36:57 +00:00
|
|
|
use App\Models\Song;
|
2018-08-19 14:56:56 +00:00
|
|
|
use Illuminate\Cache\Repository as Cache;
|
2017-01-06 03:04:08 +00:00
|
|
|
|
2018-08-19 14:56:56 +00:00
|
|
|
class MediaCacheService
|
2017-01-06 03:04:08 +00:00
|
|
|
{
|
2018-08-24 15:27:19 +00:00
|
|
|
private const CACHE_KEY = 'media_cache';
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
public function __construct(private Cache $cache)
|
2018-08-19 14:56:56 +00:00
|
|
|
{
|
|
|
|
}
|
2017-01-06 03:04:08 +00:00
|
|
|
|
2017-08-05 21:51:59 +00:00
|
|
|
/**
|
2017-08-05 22:27:41 +00:00
|
|
|
* Get media data.
|
2017-08-05 21:51:59 +00:00
|
|
|
* If caching is enabled, the data will be retrieved from the cache.
|
2017-08-05 22:27:41 +00:00
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @return array<mixed>
|
2017-08-05 21:51:59 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function get(): array
|
2017-01-06 03:04:08 +00:00
|
|
|
{
|
2017-01-15 04:27:05 +00:00
|
|
|
if (!config('koel.cache_media')) {
|
2017-04-28 14:36:57 +00:00
|
|
|
return $this->query();
|
2017-01-15 04:27:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
return $this->cache->rememberForever(self::CACHE_KEY, fn (): array => $this->query());
|
2017-01-06 03:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 14:36:57 +00:00
|
|
|
/**
|
|
|
|
* Query fresh data from the database.
|
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @return array<mixed>
|
2017-04-28 14:36:57 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
private function query(): array
|
2017-04-28 14:36:57 +00:00
|
|
|
{
|
|
|
|
return [
|
2022-08-09 18:45:11 +00:00
|
|
|
'albums' => Album::query()->orderBy('name')->get(),
|
|
|
|
'artists' => Artist::query()->orderBy('name')->get(),
|
2017-04-28 14:36:57 +00:00
|
|
|
'songs' => Song::all(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function clear(): void
|
2017-01-06 03:04:08 +00:00
|
|
|
{
|
2018-08-24 15:27:19 +00:00
|
|
|
$this->cache->forget(self::CACHE_KEY);
|
2017-01-06 03:04:08 +00:00
|
|
|
}
|
|
|
|
}
|