Skip to content

FOUR-20492 retrieve settings using cache #7783

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

Merged
merged 5 commits into from
Dec 4, 2024
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 ProcessMaker/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ interface CacheInterface
*/
public function get(string $key, mixed $default = null): mixed;

/**
* Fetches a value from the cache, or stores the value from the callback if the key exists.
*
* @param string $key The unique key of this item in the cache.
* @param callable $callback The callback that will return the value to store in the cache.
*
* @return mixed The value of the item from the cache, or $default in case of cache miss.
*
* @throws \InvalidArgumentException
*/
public function getOrCache(string $key, callable $callback): mixed;

/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
*
Expand Down
37 changes: 31 additions & 6 deletions ProcessMaker/Cache/Settings/SettingCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace ProcessMaker\Cache\Settings;

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

class SettingCacheManager implements CacheInterface
Expand Down Expand Up @@ -38,13 +36,40 @@ public function __call($method, $arguments): mixed
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->cacheManager->get($key, $default);
}

/**
* Get a value from the settings cache, or store the value from the callback if the key exists.
*
* @param string $key
* @param callable $callback
*
* @return mixed
*
* @throws \InvalidArgumentException
*/
public function getOrCache(string $key, callable $callback): mixed
{
$value = $this->get($key);

if ($value !== null) {
return $value;
}

try {
return $this->cacheManager->get($key, $default);
} catch (Exception $e) {
Log::error('Cache error: ' . $e->getMessage());
$value = $callback();

if ($value === null) {
throw new \InvalidArgumentException('The key does not exist.');
}
} catch (\Exception $e) {
throw new \InvalidArgumentException('The key does not exist.');
}

return null;
$this->set($key, $value);

return $value;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions ProcessMaker/Http/Controllers/Api/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ public function update(Setting $setting, Request $request)
// Register the Event
SettingsUpdated::dispatch($setting, $setting->getChanges(), $original);

// Store the setting in the cache
\SettingCache::set($setting->key, $setting->refresh());

return response([], 204);
}

Expand Down
13 changes: 12 additions & 1 deletion ProcessMaker/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,18 @@ public static function messages()
*/
public static function byKey(string $key)
{
return (new self)->where('key', $key)->first();
$setting = \SettingCache::get($key);

if ($setting === null) {
$setting = (new self)->where('key', $key)->first();

// Store the setting in the cache if it exists
if ($setting !== null) {
\SettingCache::set($key, $setting);
}
}

return $setting;
}

/**
Expand Down
102 changes: 102 additions & 0 deletions tests/Feature/Cache/SettingCacheManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Tests\Feature\Cache;

use Tests\TestCase;

class SettingCacheManagerTest extends TestCase
{
public function testGet()
{
$key = 'test_key';
$default = 'default_value';
$expected = 'cached_value';

\SettingCache::shouldReceive('get')
->with($key, $default)
->andReturn($expected);

$result = \SettingCache::get($key, $default);

$this->assertEquals($expected, $result);
}

public function testSet()
{
$key = 'test_key';
$value = 'test_value';
$ttl = 60;

\SettingCache::shouldReceive('set')
->with($key, $value, $ttl)
->andReturn(true);

$result = \SettingCache::set($key, $value, $ttl);

$this->assertTrue($result);
}

public function testDelete()
{
$key = 'test_key';

\SettingCache::shouldReceive('delete')
->with($key)
->andReturn(true);

$result = \SettingCache::delete($key);

$this->assertTrue($result);
}

public function testClear()
{
\SettingCache::shouldReceive('clear')
->andReturn(true);

$result = \SettingCache::clear();

$this->assertTrue($result);
}

public function testHas()
{
$key = 'test_key';

\SettingCache::shouldReceive('has')
->with($key)
->andReturn(true);

$result = \SettingCache::has($key);

$this->assertTrue($result);
}

public function testMissing()
{
$key = 'test_key';

\SettingCache::shouldReceive('missing')
->with($key)
->andReturn(false);

$result = \SettingCache::missing($key);

$this->assertFalse($result);
}

public function testCall()
{
$method = 'add';
$arguments = ['arg1', 'arg2'];
$expected = 'cached_value';

\SettingCache::shouldReceive($method)
->with(...$arguments)
->andReturn($expected);

$result = \SettingCache::__call($method, $arguments);

$this->assertEquals($expected, $result);
}
}
132 changes: 74 additions & 58 deletions tests/Feature/Cache/SettingCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,117 @@

namespace Tests\Feature\Cache;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use ProcessMaker\Models\Setting;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;

class SettingCacheTest extends TestCase
{
public function testGet()
{
$key = 'test_key';
$default = 'default_value';
$expected = 'cached_value';

\SettingCache::shouldReceive('get')
->with($key, $default)
->andReturn($expected);

$result = \SettingCache::get($key, $default);
use RequestHelper;
use RefreshDatabase;

$this->assertEquals($expected, $result);
private function upgrade()
{
$this->artisan('migrate', [
'--path' => 'upgrades/2023_11_30_185738_add_password_policies_settings.php',
])->run();
}

public function testSet()
public static function trackQueries(): void
{
$key = 'test_key';
$value = 'test_value';
$ttl = 60;
DB::enableQueryLog();
}

\SettingCache::shouldReceive('set')
->with($key, $value, $ttl)
->andReturn(true);
public static function flushQueryLog(): void
{
DB::flushQueryLog();
}

$result = \SettingCache::set($key, $value, $ttl);
public static function getQueriesExecuted(): array
{
return DB::getQueryLog();
}

$this->assertTrue($result);
public static function getQueryCount(): int
{
return count(self::getQueriesExecuted());
}

public function testDelete()
public function testGetSettingByKeyCached(): void
{
$key = 'test_key';
$this->upgrade();

$key = 'password-policies.users_can_change';

\SettingCache::shouldReceive('delete')
->with($key)
->andReturn(true);
$setting = Setting::where('key', $key)->first();
\SettingCache::set($key, $setting);

$result = \SettingCache::delete($key);
$this->trackQueries();

$this->assertTrue($result);
$setting = Setting::byKey($key);

$this->assertEquals(0, self::getQueryCount());
$this->assertEquals($key, $setting->key);
}

public function testClear()
public function testGetSettingByKeyNotCached(): void
{
\SettingCache::shouldReceive('clear')
->andReturn(true);
$key = 'password-policies.uppercase';

$this->upgrade();
$this->trackQueries();

$result = \SettingCache::clear();
$setting = Setting::byKey($key);

$this->assertTrue($result);
$this->assertEquals(1, self::getQueryCount());
$this->assertEquals($key, $setting->key);

$this->flushQueryLog();

$setting = Setting::byKey($key);
$this->assertEquals(0, self::getQueryCount());
$this->assertNotNull($setting);
$this->assertEquals($key, $setting->key);
}

public function testHas()
public function testGetSettingByKeyCachedAfterUpdate(): void
{
$key = 'test_key';
$key = 'password-policies.special';

\SettingCache::shouldReceive('has')
->with($key)
->andReturn(true);
$this->upgrade();
$this->trackQueries();

$result = \SettingCache::has($key);
$setting = Setting::byKey($key);

$this->assertTrue($result);
}
$this->assertEquals(1, self::getQueryCount());
$this->assertEquals($key, $setting->key);
$this->assertEquals($setting->config, 1);

public function testMissing()
{
$key = 'test_key';
$data = array_merge($setting->toArray(), ['config' => false]);

\SettingCache::shouldReceive('missing')
->with($key)
->andReturn(false);
$response = $this->apiCall('PUT', route('api.settings.update', ['setting' => $setting->id]), $data);
$response->assertStatus(204);

$result = \SettingCache::missing($key);
$this->flushQueryLog();

$this->assertFalse($result);
$setting = Setting::byKey($key);
$this->assertEquals(0, self::getQueryCount());
$this->assertEquals($key, $setting->key);
$this->assertEquals($setting->config, 0);
}

public function testCall()
public function testGetSettingByNotExistingKey()
{
$method = 'add';
$arguments = ['arg1', 'arg2'];
$expected = 'cached_value';
$this->withoutExceptionHandling();
$key = 'non-existing-key';

\SettingCache::shouldReceive($method)
->with(...$arguments)
->andReturn($expected);
$callback = fn() => Setting::where('key', $key)->first();

$result = \SettingCache::__call($method, $arguments);
$this->expectException(\InvalidArgumentException::class);
$setting = \SettingCache::getOrCache($key, $callback);

$this->assertEquals($expected, $result);
$this->assertNull($setting);
}
}
Loading