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-19 14:56:56 +00:00
|
|
|
private $cache;
|
|
|
|
private $keyName = 'media_cache';
|
|
|
|
|
|
|
|
public function __construct(Cache $cache)
|
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
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
|
|
|
*
|
2017-08-05 21:51:59 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-01-06 03:04:08 +00:00
|
|
|
public function get()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2018-08-19 14:56:56 +00:00
|
|
|
return $this->cache->rememberForever($this->keyName, function () {
|
2017-08-05 21:51:59 +00:00
|
|
|
return $this->query();
|
|
|
|
});
|
2017-01-06 03:04:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 14:36:57 +00:00
|
|
|
/**
|
|
|
|
* Query fresh data from the database.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function query()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'albums' => Album::orderBy('name')->get(),
|
|
|
|
'artists' => Artist::orderBy('name')->get(),
|
|
|
|
'songs' => Song::all(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-08-05 21:51:59 +00:00
|
|
|
/**
|
|
|
|
* Clear the media cache.
|
|
|
|
*/
|
2017-01-06 03:04:08 +00:00
|
|
|
public function clear()
|
|
|
|
{
|
2018-08-19 14:56:56 +00:00
|
|
|
$this->cache->forget($this->keyName);
|
2017-01-06 03:04:08 +00:00
|
|
|
}
|
|
|
|
}
|