-
Notifications
You must be signed in to change notification settings - Fork 227
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ecd24d
feat: add a custom cache class structure
devmiguelangel e4deb5a
test: add tests for SettingCache methods
devmiguelangel b2b983d
feat: organize cache classes
devmiguelangel 0470d27
Merge branch 'epic/FOUR-20326' into FOUR-20467
devmiguelangel 2a1b3b5
fix(cr): enhance settings cache error handling and configuration check
devmiguelangel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.