Update file size caching for tracks

This commit is contained in:
Kelvin Zhang 2015-10-29 16:10:38 +00:00
parent 5a81908ed4
commit 4f9046af6e
2 changed files with 17 additions and 20 deletions

View file

@ -433,16 +433,7 @@ class Track extends Model
public function getFilesize($formatName) public function getFilesize($formatName)
{ {
return Cache::remember($this->getCacheKey('filesize-' . $formatName), 1440, function () use ($formatName) { return $this->trackFiles()->where('format', $formatName)->first()->filesize;
$file = $this->getFileFor($formatName);
$size = 0;
if (is_file($file)) {
$size = filesize($file);
}
return $size;
});
} }
public function canView($user) public function canView($user)

View file

@ -24,6 +24,7 @@ use Helpers;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\URL; use Illuminate\Support\Facades\URL;
class TrackFile extends Model class TrackFile extends Model
@ -96,16 +97,7 @@ class TrackFile extends Model
protected function getFilesize() protected function getFilesize()
{ {
return Cache::remember($this->getCacheKey('filesize'), 1440, function () { return $this->filesize;
$file = $this->getFile();
$size = 0;
if (is_file($file)) {
$size = filesize($file);
}
return $size;
});
} }
public function getDirectory() public function getDirectory()
@ -134,4 +126,18 @@ class TrackFile extends Model
{ {
return 'track_file-' . $this->id . '-' . $key; return 'track_file-' . $this->id . '-' . $key;
} }
public function updateFilesize()
{
$file = $this->getFile();
if (File::exists($file)) {
$size = File::size($file);
} else {
$size = null;
}
$this->filesize = $size;
$this->update();
}
} }