koel/app/Services/MediaCache.php

48 lines
870 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';
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-01-06 03:04:08 +00:00
$data = Cache::get($this->keyName);
if (!$data) {
$data = $this->query();
2017-01-06 03:04:08 +00:00
Cache::forever($this->keyName, $data);
}
return $data;
}
/**
* 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-01-06 03:04:08 +00:00
public function clear()
{
Cache::forget($this->keyName);
}
}