Skip to content

Commit 1952496

Browse files
committed
peer review suggestions added
1 parent 293b286 commit 1952496

4 files changed

Lines changed: 16 additions & 20 deletions

File tree

src/Memoize.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +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.
13-
* @param bool $shouldUpdate Sets whether the cache should update itself during the call.
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 $refresh Determines if cache should be refreshed.
1414
*
1515
* @return mixed The data requested, optionally pulled from cache
1616
*/
17-
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, bool $shouldUpdate = false);
17+
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, bool $refresh = false);
1818
}

src/Memory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class Memory implements Memoize
2222
* @param string $key
2323
* @param callable $compute
2424
* @param int|null $cacheTime
25-
* @param bool $shouldUpdate
25+
* @param bool $refresh
2626
*
2727
* @return mixed
2828
*/
29-
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, $shouldUpdate = false)
29+
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, bool $refresh = false)
3030
{
31-
if (array_key_exists($key, $this->cache)) {
31+
if (array_key_exists($key, $this->cache) && !$refresh) {
3232
return $this->cache[$key];
3333
}
3434

src/None.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class None implements Memoize
1616
* @param string $key
1717
* @param callable $compute
1818
* @param int|null $cacheTime
19-
* @param bool $shouldUpdate
19+
* @param bool $refresh
2020
*
2121
* @return mixed
2222
*/
23-
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, $shouldUpdate = false)
23+
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, bool $refresh = false)
2424
{
2525
return call_user_func($compute);
2626
}

src/Predis.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ public function __construct(
6565
* @param string $key
6666
* @param callable $compute
6767
* @param int|null $cacheTime
68-
* @param bool $shouldUpdate
68+
* @param bool $refresh
6969
*
7070
* @return mixed
7171
*/
72-
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, bool $shouldUpdate = false)
72+
public function memoizeCallable(string $key, callable $compute, int $cacheTime = null, bool $refresh = false)
7373
{
74-
if (!$this->refresh) {
74+
if (!$this->refresh && !$refresh) {
7575
try {
7676
if (rand(1, 100) <= $this->refreshPercent) {
7777
// {$refreshPercent}% of requests should check to see if this key is almost expired.
@@ -83,14 +83,10 @@ public function memoizeCallable(string $key, callable $compute, int $cacheTime =
8383
return $this->getData($key, $compute, $cacheTime);
8484
}
8585
}
86-
8786
$cached = $this->client->get($key);
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-
}
87+
if ($cached !== null) {
88+
$data = json_decode($cached, true);
89+
return $data['result'];
9490
}
9591
} catch (\Exception $e) {
9692
return $this->getData($key, $compute, $cacheTime);

0 commit comments

Comments
 (0)