Skip to content

Commit 667db12

Browse files
feat: enhance setting cache manager
1 parent 349b512 commit 667db12

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

ProcessMaker/Cache/CacheInterface.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,23 @@ interface CacheInterface
88
* Fetches a value from the cache.
99
*
1010
* @param string $key The unique key of this item in the cache.
11+
* @param mixed $default Default value to return if the key does not exist.
12+
*
13+
* @return mixed The value of the item from the cache, or $default in case of cache miss.
14+
*/
15+
public function get(string $key, mixed $default = null): mixed;
16+
17+
/**
18+
* Fetches a value from the cache, or stores the value from the callback if the key exists.
19+
*
20+
* @param string $key The unique key of this item in the cache.
1121
* @param callable $callback The callback that will return the value to store in the cache.
1222
*
1323
* @return mixed The value of the item from the cache, or $default in case of cache miss.
24+
*
25+
* @throws \InvalidArgumentException
1426
*/
15-
public function get(string $key, callable $callback = null): mixed;
27+
public function getOrCache(string $key, callable $callback): mixed;
1628

1729
/**
1830
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.

ProcessMaker/Cache/Settings/SettingCacheManager.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace ProcessMaker\Cache\Settings;
44

5-
use Exception;
65
use Illuminate\Cache\CacheManager;
7-
use Illuminate\Support\Facades\Log;
86
use ProcessMaker\Cache\CacheInterface;
97

108
class SettingCacheManager implements CacheInterface
@@ -32,28 +30,45 @@ public function __call($method, $arguments): mixed
3230
* Get a value from the settings cache.
3331
*
3432
* @param string $key
35-
* @param callable|null $callback
33+
* @param mixed $default
3634
*
3735
* @return mixed
3836
*/
39-
public function get(string $key, callable $callback = null): mixed
37+
public function get(string $key, mixed $default = null): mixed
4038
{
41-
$value = $this->cacheManager->get($key);
39+
return $this->cacheManager->get($key, $default);
40+
}
4241

43-
if ($value) {
44-
return $value;
45-
}
42+
/**
43+
* Get a value from the settings cache, or store the value from the callback if the key exists.
44+
*
45+
* @param string $key
46+
* @param callable $callback
47+
*
48+
* @return mixed
49+
*
50+
* @throws \InvalidArgumentException
51+
*/
52+
public function getOrCache(string $key, callable $callback): mixed
53+
{
54+
$value = $this->get($key);
4655

47-
if ($callback === null) {
48-
return null;
56+
if ($value !== null) {
57+
return $value;
4958
}
5059

51-
$value = $callback();
60+
try {
61+
$value = $callback();
5262

53-
if ($value === null) {
63+
if ($value === null) {
64+
throw new \InvalidArgumentException('The key does not exist.');
65+
}
66+
} catch (\Exception $e) {
5467
throw new \InvalidArgumentException('The key does not exist.');
5568
}
5669

70+
$this->set($key, $value);
71+
5772
return $value;
5873
}
5974

ProcessMaker/Models/Setting.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,16 @@ public static function messages()
142142
* Get setting by key
143143
*
144144
* @param string $key
145-
* @param bool $withCallback
146145
*
147146
* @return \ProcessMaker\Models\Setting|null
148147
* @throws \Exception
149148
*/
150-
public static function byKey(string $key, bool $withCallback = false)
149+
public static function byKey(string $key)
151150
{
152-
$callback = null;
151+
$setting = \SettingCache::get($key);
153152

154-
if ($withCallback) {
155-
$callback = fn() => (new self)->where('key', $key)->first();
156-
}
157-
158-
$setting = \SettingCache::get($key, $callback);
159-
160-
if (!is_null($setting)) {
153+
if ($setting === null) {
154+
$setting = (new self)->where('key', $key)->first();
161155
\SettingCache::set($key, $setting);
162156
}
163157

tests/Feature/Cache/SettingCacheTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ class SettingCacheTest extends TestCase
1313
use RequestHelper;
1414
use RefreshDatabase;
1515

16-
/* public function setUp(): void
17-
{
18-
parent::setUp();
19-
} */
20-
2116
private function upgrade()
2217
{
2318
$this->artisan('migrate', [
@@ -65,12 +60,11 @@ public function testGetSettingByKeyCached(): void
6560
public function testGetSettingByKeyNotCached(): void
6661
{
6762
$key = 'password-policies.uppercase';
68-
\SettingCache::delete($key);
6963

7064
$this->upgrade();
7165
$this->trackQueries();
7266

73-
$setting = Setting::byKey($key, true);
67+
$setting = Setting::byKey($key);
7468

7569
$this->assertEquals(1, self::getQueryCount());
7670
$this->assertEquals($key, $setting->key);
@@ -86,12 +80,11 @@ public function testGetSettingByKeyNotCached(): void
8680
public function testGetSettingByKeyCachedAfterUpdate(): void
8781
{
8882
$key = 'password-policies.special';
89-
\SettingCache::delete($key);
9083

9184
$this->upgrade();
9285
$this->trackQueries();
9386

94-
$setting = Setting::byKey($key, true);
87+
$setting = Setting::byKey($key);
9588

9689
$this->assertEquals(1, self::getQueryCount());
9790
$this->assertEquals($key, $setting->key);
@@ -116,7 +109,8 @@ public function testGetSettingByNotExistingKey()
116109
$key = 'non-existing-key';
117110

118111
$this->expectException(\InvalidArgumentException::class);
119-
$setting = Setting::byKey($key, true);
112+
$callback = fn() => Setting::where('key', $key)->first();
113+
$setting = \SettingCache::getOrCache($key, $callback);
120114

121115
$this->assertNull($setting);
122116
}

0 commit comments

Comments
 (0)