Skip to content

Commit 293b286

Browse files
committed
added shouldUpdate flag to allow cache to be refreshed
1 parent 6fe6df1 commit 293b286

4 files changed

Lines changed: 17 additions & 10 deletions

File tree

src/Memoize.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ interface Memoize
77
/**
88
* Gets the value stored in the cache or uses the passed function to compute the value and save to cache.
99
*
10-
* @param string $key The key to fetch
11-
* @param callable $compute A function to run if the value was not cached that will return the result.
12-
* @param int $cacheTime The number of seconds to cache the response for, or null to not expire it ever.
10+
* @param string $key The key to fetch
11+
* @param callable $compute A function to run if the value was not cached that will return the result.
12+
* @param int $cacheTime The number of seconds to cache the response for, or null to not expire it ever.
13+
* @param bool $shouldUpdate Sets whether the cache should update itself during the call.
1314
*
1415
* @return mixed The data requested, optionally pulled from cache
1516
*/
16-
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null);
17+
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, bool $shouldUpdate = false);
1718
}

src/Memory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ class Memory implements Memoize
2222
* @param string $key
2323
* @param callable $compute
2424
* @param int|null $cacheTime
25+
* @param bool $shouldUpdate
2526
*
2627
* @return mixed
2728
*/
28-
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null)
29+
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, $shouldUpdate = false)
2930
{
3031
if (array_key_exists($key, $this->cache)) {
3132
return $this->cache[$key];

src/None.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ class None implements Memoize
1616
* @param string $key
1717
* @param callable $compute
1818
* @param int|null $cacheTime
19+
* @param bool $shouldUpdate
1920
*
2021
* @return mixed
2122
*/
22-
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null)
23+
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, $shouldUpdate = false)
2324
{
2425
return call_user_func($compute);
2526
}

src/Predis.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ public function __construct(
6565
* @param string $key
6666
* @param callable $compute
6767
* @param int|null $cacheTime
68+
* @param bool $shouldUpdate
6869
*
6970
* @return mixed
7071
*/
71-
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null)
72+
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, bool $shouldUpdate = false)
7273
{
7374
if (!$this->refresh) {
7475
try {
@@ -84,9 +85,12 @@ public function memoizeCallable(string $key, callable $compute, int $cacheTime =
8485
}
8586

8687
$cached = $this->client->get($key);
87-
if ($cached !== null) {
88-
$data = json_decode($cached, true);
89-
return $data['result'];
88+
if ($shouldUpdate === false) {
89+
$cached = $this->client->get($key);
90+
if ($cached !== null) {
91+
$data = json_decode($cached, true);
92+
return $data['result'];
93+
}
9094
}
9195
} catch (\Exception $e) {
9296
return $this->getData($key, $compute, $cacheTime);

0 commit comments

Comments
 (0)