koel/app/Services/MediaCache.php

53 lines
985 B
PHP
Raw Normal View History

2017-01-06 03:04:08 +00:00
<?php
namespace App\Services;
use App\Models\Album;
2017-01-06 03:04:08 +00:00
use App\Models\Artist;
use App\Models\Song;
2017-01-06 03:04:08 +00:00
use Cache;
class MediaCache
{
protected $keyName = 'media_cache';
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')) {
return $this->query();
2017-01-15 04:27:05 +00:00
}
2017-08-05 21:51:59 +00:00
return Cache::rememberForever($this->keyName, function () {
return $this->query();
});
2017-01-06 03:04:08 +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()
{
Cache::forget($this->keyName);
}
}