diff --git a/ProcessMaker/Cache/CacheInterface.php b/ProcessMaker/Cache/CacheInterface.php new file mode 100644 index 0000000000..a89024c9d5 --- /dev/null +++ b/ProcessMaker/Cache/CacheInterface.php @@ -0,0 +1,63 @@ +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); + } +} diff --git a/ProcessMaker/Providers/ProcessMakerServiceProvider.php b/ProcessMaker/Providers/ProcessMakerServiceProvider.php index ce11b8e437..4c8af989f3 100644 --- a/ProcessMaker/Providers/ProcessMakerServiceProvider.php +++ b/ProcessMaker/Providers/ProcessMakerServiceProvider.php @@ -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; @@ -29,6 +30,7 @@ use ProcessMaker\Models; use ProcessMaker\Observers; use ProcessMaker\PolicyExtension; +use RuntimeException; /** * Provide our ProcessMaker specific services. @@ -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.'); + } + }); } /** diff --git a/config/app.php b/config/app.php index a011248e48..07a788a4b1 100644 --- a/config/app.php +++ b/config/app.php @@ -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' => [ @@ -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', diff --git a/tests/Feature/Cache/SettingCacheTest.php b/tests/Feature/Cache/SettingCacheTest.php new file mode 100644 index 0000000000..56288bec63 --- /dev/null +++ b/tests/Feature/Cache/SettingCacheTest.php @@ -0,0 +1,102 @@ +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); + } +}