Cache the media

This commit is contained in:
An Phan 2017-01-06 11:04:08 +08:00
parent 43d2958ac6
commit f51e660501
No known key found for this signature in database
GPG key ID: 05536BB4BCDC02A2
10 changed files with 109 additions and 2 deletions

View file

@ -7,6 +7,7 @@ use DB;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use MediaCache;
class Init extends Command
{
@ -60,6 +61,8 @@ class Init extends Command
$this->info('Migrating database');
Artisan::call('migrate', ['--force' => true]);
// Clean the media cache, just in case we did any media-related migration
MediaCache::clear();
if (!User::count()) {
$this->info('Seeding initial data');

View file

@ -0,0 +1,13 @@
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class MediaCache extends Facade
{
protected static function getFacadeAccessor()
{
return 'MediaCache';
}
}

View file

@ -3,13 +3,13 @@
namespace App\Http\Controllers\API;
use App\Application;
use App\Models\Artist;
use App\Models\Interaction;
use App\Models\Playlist;
use App\Models\Setting;
use App\Models\User;
use iTunes;
use Lastfm;
use MediaCache;
use YouTube;
class DataController extends Controller
@ -29,7 +29,7 @@ class DataController extends Controller
}
return response()->json([
'artists' => Artist::orderBy('name')->with('albums', with('albums.songs'))->get(),
'artists' => MediaCache::get(),
'settings' => auth()->user()->is_admin ? Setting::pluck('value', 'key')->all() : [],
'playlists' => $playlists,
'interactions' => Interaction::byCurrentUser()->get(),

View file

@ -0,0 +1,24 @@
<?php
namespace App\Listeners;
use MediaCache;
class ClearMediaCache
{
/**
* Create the event listener.
*/
public function __construct()
{
}
/**
* Fired every time a LibraryChanged event is triggered.
* Clears the media cache.
*/
public function handle()
{
MediaCache::clear();
}
}

View file

@ -25,6 +25,7 @@ class EventServiceProvider extends ServiceProvider
'App\Events\LibraryChanged' => [
'App\Listeners\TidyLibrary',
'App\Listeners\ClearMediaCache',
],
];

View file

@ -0,0 +1,31 @@
<?php
namespace App\Providers;
use App\Services\MediaCache;
use Illuminate\Support\ServiceProvider;
class MediaCacheServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
app()->singleton('MediaCache', function () {
return new MediaCache();
});
}
}

View file

@ -10,6 +10,7 @@ use App\Models\Artist;
use App\Models\File;
use App\Models\Setting;
use App\Models\Song;
use Cache;
use getID3;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Finder\Finder;
@ -157,7 +158,10 @@ class Media
// File format etc. will be handled by File::sync().
elseif ($record->isNewOrModified()) {
$result = (new File($path))->sync($this->tags);
Log::info($result instanceof Song ? "Synchronized $path" : "Invalid file $path");
event(new LibraryChanged());
}
return;

View file

@ -0,0 +1,27 @@
<?php
namespace App\Services;
use App\Models\Artist;
use Cache;
class MediaCache
{
protected $keyName = 'media_cache';
public function get()
{
$data = Cache::get($this->keyName);
if (!$data) {
$data = Artist::orderBy('name')->with('albums', with('albums.songs'))->get();
Cache::forever($this->keyName, $data);
}
return $data;
}
public function clear()
{
Cache::forget($this->keyName);
}
}

View file

@ -152,6 +152,7 @@ return [
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\MediaServiceProvider::class,
App\Providers\MediaCacheServiceProvider::class,
App\Providers\UtilServiceProvider::class,
App\Providers\LastfmServiceProvider::class,
App\Providers\YouTubeServiceProvider::class,
@ -207,6 +208,7 @@ return [
'View' => Illuminate\Support\Facades\View::class,
'Media' => App\Facades\Media::class,
'MediaCache' => App\Facades\MediaCache::class,
'Util' => App\Facades\Util::class,
'Lastfm' => App\Facades\Lastfm::class,
'YouTube' => App\Facades\YouTube::class,

View file

@ -156,6 +156,8 @@ class MediaTest extends TestCase
public function testWatchSingleFileAdded()
{
$this->expectsEvents(LibraryChanged::class);
$path = $this->mediaPath.'/blank.mp3';
(new Media())->syncByWatchRecord(new InotifyWatchRecord("CLOSE_WRITE,CLOSE $path"));