Skip to content

Commit

Permalink
Add automatic cache clearing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarpsvo committed Apr 29, 2022
1 parent 2f33e6d commit ae56298
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public function __construct(array $attributes = [])
$this->setTable(NovaSettings::getSettingsTableName());
}

protected static function booted()
{
static::updated(function ($setting) {
NovaSettings::getStore()->clearCache($setting->key);
});
}

public function setValueAttribute($value)
{
$this->attributes['value'] = is_array($value) ? json_encode($value) : $value;
Expand Down
2 changes: 1 addition & 1 deletion src/NovaSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static function doesPathExist($path)
return array_key_exists($path, static::getFields());
}

protected static function getStore(): NovaSettingsStore
public static function getStore(): NovaSettingsStore
{
return app()->make(NovaSettingsStore::class);
}
Expand Down
15 changes: 15 additions & 0 deletions src/NovaSettingsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ public function setSettingValue($settingKey, $value = null)
return $setting;
}

public function clearCache($keyNames = null)
{
// Clear whole cache
if (empty($keyNames)) {
$this->cache = [];
return;
}

// Clear specific keys
if (is_string($keyNames)) $keyNames = [$keyNames];
foreach ($keyNames as $key) {
unset($this->cache[$key]);
}
}

public function clearFields()
{
$this->fields = [];
Expand Down

0 comments on commit ae56298

Please sign in to comment.