Skip to content
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
21 changes: 20 additions & 1 deletion ProcessMaker/Cache/Settings/SettingCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ProcessMaker\Cache\Settings;

use Illuminate\Cache\CacheManager;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use ProcessMaker\Cache\CacheInterface;

Expand Down Expand Up @@ -156,7 +157,7 @@ public function clearBy(string $pattern): void
Redis::connection($connection)->del($matchedKeys);
}
} catch (\Exception $e) {
\Log::error('SettingCacheException' . $e->getMessage());
Log::error('SettingCacheException' . $e->getMessage());

throw new SettingCacheException('Failed to delete keys.');
}
Expand Down Expand Up @@ -185,4 +186,22 @@ public function missing(string $key): bool
{
return !$this->has($key);
}

/**
* Invalidate a value in the settings cache.
*
* @param string $key
*
* @return void
*/
public function invalidate(string $key): void
{
try {
$this->cacheManager->forget($key);
} catch (\Exception $e) {
Log::error($e->getMessage());

throw new SettingCacheException('Failed to invalidate cache KEY:' . $key);
}
}
}
13 changes: 13 additions & 0 deletions ProcessMaker/Observers/SettingObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,18 @@ public function saving(Setting $setting)
$setting->config = $return;
break;
}

\SettingCache::invalidate($setting->key);
}

/**
* Handle the setting "deleted" event.
*
* @param \ProcessMaker\Models\Setting $setting
* @return void
*/
public function deleted(Setting $setting): void
{
\SettingCache::invalidate($setting->key);
}
}
61 changes: 61 additions & 0 deletions tests/Feature/Cache/SettingCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,65 @@ public function testClearOnlySettings()
config()->set('cache.default', 'array');
$this->assertEquals(3, Cache::get('password-policies.uppercase'));
}

public function testInvalidateOnSaved()
{
$setting = Setting::factory()->create([
'key' => 'password-policies.users_can_change',
'config' => 1,
'format' => 'boolean',
]);

\SettingCache::set($setting->key, $setting);
$settingCache = \SettingCache::get($setting->key);

$this->assertEquals(1, $settingCache->config);

$setting->update(['config' => 0]);
$settingCache = \SettingCache::get($setting->key);
$this->assertNull($settingCache);
}

public function testInvalidateOnDeleted()
{
$setting = Setting::factory()->create([
'key' => 'password-policies.users_can_change',
'config' => 1,
'format' => 'boolean',
]);

\SettingCache::set($setting->key, $setting);
$settingCache = \SettingCache::get($setting->key);

$this->assertEquals(1, $settingCache->config);

$setting->delete();
$settingCache = \SettingCache::get($setting->key);
$this->assertNull($settingCache);
}

public function testInvalidateWithException()
{
$setting = Setting::factory()->create([
'key' => 'password-policies.numbers',
'config' => 1,
'format' => 'boolean',
]);

\SettingCache::set($setting->key, $setting);
$settingCache = \SettingCache::get($setting->key);

$this->assertEquals(1, $settingCache->config);

\SettingCache::shouldReceive('invalidate')
->with($setting->key)
->andThrow(new SettingCacheException('Failed to invalidate cache KEY:' . $setting->key))
->once();
$this->expectException(SettingCacheException::class);
$this->expectExceptionMessage('Failed to invalidate cache KEY:' . $setting->key);

\SettingCache::shouldReceive('clear')->once()->andReturn(true);

$setting->delete();
}
}