Total Complexity | 5 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Coverage | 92.86% |
Changes | 0 |
1 | <?php |
||
10 | class MediaCacheService |
||
11 | { |
||
12 | private const CACHE_KEY = 'media_cache'; |
||
13 | |||
14 | private $cache; |
||
15 | |||
16 | 132 | public function __construct(Cache $cache) |
|
17 | { |
||
18 | 132 | $this->cache = $cache; |
|
19 | 132 | } |
|
20 | |||
21 | /** |
||
22 | * Get media data. |
||
23 | * If caching is enabled, the data will be retrieved from the cache. |
||
24 | * |
||
25 | * @return mixed[] |
||
26 | */ |
||
27 | 3 | public function get(): array |
|
28 | { |
||
29 | 3 | if (!config('koel.cache_media')) { |
|
30 | 1 | return $this->query(); |
|
31 | } |
||
32 | |||
33 | return $this->cache->rememberForever(self::CACHE_KEY, function (): array { |
||
34 | return $this->query(); |
||
35 | 2 | }); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Query fresh data from the database. |
||
40 | * |
||
41 | * @return mixed[] |
||
42 | */ |
||
43 | 1 | private function query(): array |
|
44 | { |
||
45 | return [ |
||
46 | 1 | 'albums' => Album::orderBy('name')->get(), |
|
47 | 1 | 'artists' => Artist::orderBy('name')->get(), |
|
48 | 1 | 'songs' => Song::all(), |
|
49 | ]; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Clear the media cache. |
||
54 | */ |
||
55 | 7 | public function clear(): void |
|
58 | 7 | } |
|
59 | } |
||
60 |