Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable caching of the default value if configured that way #40

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,16 @@
|
*/
'value_serializer' => \Rawilk\Settings\Support\ValueSerializers\ValueSerializer::class,

/*
|--------------------------------------------------------------------------
| Cache Default Value
|--------------------------------------------------------------------------
|
| When a setting is not persisted, we will cache the passed in default value
| if this is set to true. This may not always be desirable, so you can
| disable it here if needed.
|
*/
'cache_default_value' => true,
];
15 changes: 14 additions & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class Settings
// Meant for internal use only.
protected bool $resetContext = true;

/**
* If true, we will cache the default value for a setting when it is not persisted
* when trying to retrieve it.
*/
protected bool $cacheDefaultValue = true;

protected bool $teams = false;

/** @var null|string|int */
Expand Down Expand Up @@ -110,6 +116,13 @@ public function setTeamForeignKey(?string $foreignKey): self
return $this;
}

public function cacheDefaultValue(bool $cacheDefaultValue = true): self
{
$this->cacheDefaultValue = $cacheDefaultValue;

return $this;
}

public function forget(string|BackedEnum $key)
{
$key = $this->normalizeKey($key);
Expand Down Expand Up @@ -154,7 +167,7 @@ public function get(string|BackedEnum $key, $default = null)
$this->getCacheKey($generatedKey),
fn () => $this->driver->get(
key: $generatedKey,
default: $default,
default: $this->cacheDefaultValue ? $default : null,
teamId: $this->teams ? $this->teamId : false,
)
);
Expand Down
2 changes: 2 additions & 0 deletions src/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ protected function registerSettings(): void

$settings->setCache($app['cache.store']);

$settings->cacheDefaultValue($app['config']['settings.cache_default_value'] ?? false);

if (config('app.key')) {
$settings->setEncrypter($app['encrypter']);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'settings.table' => 'settings',
'settings.cache' => false,
'settings.encryption' => false,
'settings.cache_default_value' => true,
]);

setDatabaseDriverConnection();
Expand Down Expand Up @@ -517,6 +518,20 @@
SettingsFacade::get(InvalidEnumTypeEnum::Foo);
})->throws(InvalidEnumType::class);

test('if configured to not cache default values, it does not cache the default value if the requested setting is not persisted', function () {
settings()->enableCache()->cacheDefaultValue(false);

expect(settings()->get('foo', 'default value 1'))->toBe('default value 1')
->and(settings()->get('foo', 'default value 2'))->toBe('default value 2');
});

it('caches the default value if configured to', function () {
settings()->enableCache()->cacheDefaultValue(true);

expect(settings()->get('foo', 'default value 1'))->toBe('default value 1')
->and(settings()->get('foo', 'default value 2'))->toBe('default value 1');
});

// Helpers...

function assertQueryCount(int $expected): void
Expand Down