koel/app/Services/MediaCache.php

53 lines
987 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
/**
* Get media data.
* If caching is enabled, the data will be retrieved from the cache.
*
* @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);
}
}