Skip to content

Commit

Permalink
Merge pull request #7 from SoapBox/bugfix/query-problem
Browse files Browse the repository at this point in the history
[Bugfix] Query problem
  • Loading branch information
Justin Hayes authored Oct 5, 2017
2 parents 6817ee9 + 43ac28e commit b117f9d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Repositories/CacheSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public function getMultiple(string $group, Collection $identifiers): Collection
return !$cachedValues->has($identifier);
});

$missingValues = $this->parent->getMultiple($group, $missingIdentifiers);
if ($missingIdentifiers->isNotEmpty()) {
$missingValues = $this->parent->getMultiple($group, $missingIdentifiers);
} else {
$missingValues = new Collection();
}

$this->cache->setMultiple($missingValues->mapWithKeys(function ($value, $key) use ($group) {
return [Cache::toCacheKey($group, $key) => $value];
Expand Down
26 changes: 26 additions & 0 deletions tests/Integration/Repositories/CacheSettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Tests\TestCase;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use SoapBox\Settings\Utilities\Cache;
use SoapBox\Settings\Models\SettingValue;
use SoapBox\Settings\Models\SettingDefinition;
Expand Down Expand Up @@ -98,4 +99,29 @@ public function itDeletesTheSettingsForTheIdentifierWhenStoringASetting()
$this->assertTrue($cache->has('settings.2'));
$this->assertSame('cached_value', $settings->get('setting1')->getValue());
}

/**
* @test
*/
public function itDoesNotDoQueriesWhenTheRequestedSettingIsCached()
{
$settingDefinition = factory(SettingDefinition::class)->make([
'key' => 'setting1',
]);

$cache = new ArrayCache();
$collection = new Collection();

$setting = SettingFactory::make('1', $settingDefinition);
$setting->setValue('cached_value1');
$collection->put('setting1', $setting);

$cache->set(Cache::toCacheKey('settings', '1'), $collection);

$repository = new CacheSettings(new DatabaseSettings(), $cache);

DB::enableQueryLog();
$settings = $repository->get('settings', '1');
$this->assertCount(0, DB::getQueryLog());
}
}

0 comments on commit b117f9d

Please sign in to comment.