Skip to content

Commit

Permalink
fix performance problem
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jul 26, 2015
1 parent 330bd89 commit 74a9689
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/Conversion/ConversionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,21 @@ public function getByName($name)
*/
protected function addConversionsFromRelatedModel(Media $media)
{
if ($media->model instanceof HasMedia) {
$media->model->registerMediaConversions();
static $cachedRegistrations = [];

$cacheKey = $media->model_type;

if (! isset($cachedRegistrations[$cacheKey])) {

$cachedRegistrations[$cacheKey] = [];

if ($media->model instanceof HasMedia) {
$media->model->registerMediaConversions();
$cachedRegistrations[$cacheKey] = $media->model->mediaConversions;
}
}

$this->items = $media->model->mediaConversions;
$this->items = $cachedRegistrations[$cacheKey];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Performance/PerformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class PerformanceTest extends TestCase
/**
* @test
*/
public function it_will_not_reload_media_if_it_is_eagerly_loaded()
public function it_will_can_use_eagerly_loaded_media()
{
foreach (range(1, 10) as $index) {
$testModel = $this->testModelWithConversion->create(['name' => "test{$index}"]);
$testModel->addMedia($this->getTestFilesDirectory('test.jpg'), 'default', false);
$testModel->addMedia($this->getTestFilesDirectory('test.jpg'), 'images', false);
}

DB::connection()->enableQueryLog();
Expand All @@ -23,7 +23,7 @@ public function it_will_not_reload_media_if_it_is_eagerly_loaded()
$testModels->load("media");

foreach ($testModels as $testModel) {
$testModel->getFirstMediaUrl();
$testModel->getFirstMediaUrl('images', 'thumb');
}

$this->assertCount(2, DB::getQueryLog());
Expand Down

0 comments on commit 74a9689

Please sign in to comment.