Skip to content

FOUR-20467 implement a caching system for settings #7779

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
Nov 29, 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
63 changes: 63 additions & 0 deletions ProcessMaker/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace ProcessMaker\Cache;

interface CacheInterface
{
/**
* Fetches a value from the cache.
*
* @param string $key The unique key of this item in the cache.
* @param mixed $default Default value to return if the key does not exist.
*
* @return mixed The value of the item from the cache, or $default in case of cache miss.
*/
public function get(string $key, mixed $default = null): mixed;

/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*/
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool;

/**
* Delete an item from the cache by its unique key.
*
* @param string $key The unique cache key of the item to delete.
*
* @return bool True if the item was successfully removed. False if there was an error.
*/
public function delete(string $key): bool;

/**
* Wipes clean the entire cache's keys.
*
* @return bool True on success and false on failure.
*/
public function clear(): bool;

/**
* Determines whether an item is present in the cache.
*
* @param string $key The unique cache key of the item to check for.
*
* @return bool True if the item is present in the cache, false otherwise.
*/
public function has(string $key): bool;

/**
* Determines whether an item is missing from the cache.
*
* @param string $key The unique cache key of the item to check for.
*
* @return bool True if the item is missing from the cache, false otherwise.
*/
public function missing(string $key): bool;
}
20 changes: 20 additions & 0 deletions ProcessMaker/Cache/Settings/SettingCacheFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ProcessMaker\Cache\Settings;

use Illuminate\Support\Facades\Facade;

/**
* Class SettingCacheFacade
*
* @mixin \ProcessMaker\Cache\Settings\SettingCacheManager
*
* @package ProcessMaker\Cache
*/
class SettingCacheFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'setting.cache';
}
}
109 changes: 109 additions & 0 deletions ProcessMaker/Cache/Settings/SettingCacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace ProcessMaker\Cache\Settings;

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

class SettingCacheManager implements CacheInterface
{
protected CacheManager $cacheManager;

public function __construct(CacheManager $cacheManager)
{
$this->cacheManager = $cacheManager;
}

/**
* Dynamically pass method calls to the cache manager.
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call($method, $arguments): mixed
{
return $this->cacheManager->$method(...$arguments);
}

/**
* Get a value from the settings cache.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get(string $key, mixed $default = null): mixed
{
try {
return $this->cacheManager->get($key, $default);
} catch (Exception $e) {
Log::error('Cache error: ' . $e->getMessage());
}

return null;
}

/**
* Store a value in the settings cache.
*
* @param string $key
* @param mixed $value
* @param null|int|\DateInterval $ttl
*
* @return bool
*/
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
return $this->cacheManager->put($key, $value, $ttl);
}

/**
* Delete a value from the settings cache.
*
* @param string $key
*
* @return bool
*/
public function delete(string $key): bool
{
return $this->cacheManager->forget($key);
}

/**
* Clear the settings cache.
*
* @return bool
*/
public function clear(): bool
{
return $this->cacheManager->flush();
}

/**
* Check if a value exists in the settings cache.
*
* @param string $key
*
* @return bool
*/
public function has(string $key): bool
{
return $this->cacheManager->has($key);
}

/**
* Check if a value is missing from the settings cache.
*
* @param string $key
*
* @return bool
*/
public function missing(string $key): bool
{
return !$this->has($key);
}
}
10 changes: 10 additions & 0 deletions ProcessMaker/Providers/ProcessMakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Laravel\Horizon\Horizon;
use Laravel\Passport\Passport;
use Lavary\Menu\Menu;
use ProcessMaker\Cache\Settings\SettingCacheManager;
use ProcessMaker\Console\Migration\ExtendedMigrateCommand;
use ProcessMaker\Events\ActivityAssigned;
use ProcessMaker\Events\ScreenBuilderStarting;
Expand All @@ -29,6 +30,7 @@
use ProcessMaker\Models;
use ProcessMaker\Observers;
use ProcessMaker\PolicyExtension;
use RuntimeException;

/**
* Provide our ProcessMaker specific services.
Expand Down Expand Up @@ -164,6 +166,14 @@ public function register(): void
$this->app->singleton('compiledscreen', function ($app) {
return new ScreenCompiledManager();
});

$this->app->singleton('setting.cache', function ($app) {
if ($app['config']->get('cache.default')) {
return new SettingCacheManager($app->make('cache'));
} else {
throw new RuntimeException('Cache configuration is missing.');
}
});
}

/**
Expand Down
3 changes: 2 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
'SkinManager' => ProcessMaker\Facades\SkinManager::class,
'Theme' => Igaster\LaravelTheme\Facades\Theme::class,
'WorkspaceManager' => ProcessMaker\Facades\WorkspaceManager::class,
'SettingCache' => ProcessMaker\Cache\Settings\SettingCacheFacade::class,
])->toArray(),

'debug_blacklist' => [
Expand Down Expand Up @@ -246,7 +247,7 @@
// Process Request security log rate limit: 1 per day (86400 seconds)
'process_request_errors_rate_limit' => env('PROCESS_REQUEST_ERRORS_RATE_LIMIT', 1),
'process_request_errors_rate_limit_duration' => env('PROCESS_REQUEST_ERRORS_RATE_LIMIT_DURATION', 86400),

'default_colors' => [
'primary' => '#2773F3',
'secondary' => '#728092',
Expand Down
102 changes: 102 additions & 0 deletions tests/Feature/Cache/SettingCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Tests\Feature\Cache;

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);

$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);
}
}
Loading