Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/SoftCacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ trait SoftCacheTrait {
* @param $output mixed
*/
public function writeMethodCache($method, array $args, $output) {
$cache_key = $this->getCacheKey($args);
$this->cache[$method][$cache_key] = $output;
try {
$cache_key = $this->getCacheKey($args);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have getCacheKey() having its own try/catch block and return false if exception is thrown.

$this->cache[$method][$cache_key] = $output;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have one test case. Could you identify which line did throw the exception you found in the logs, and then stub it like here: https://stackoverflow.com/questions/45905183/mocking-methods-to-throw-error-phpunit
You may have to move $this->cache[$method][$cache_key] = $output; to a sub function.

} catch (\Exception $e) {

}
}

/**
Expand All @@ -27,10 +31,15 @@ public function writeMethodCache($method, array $args, $output) {
* @return mixed
*/
public function readMethodCache($method, array $args) {
$cache_key = $this->getCacheKey($args);
if ($this->checkMethodCache($method, $args)) {
return $this->cache[$method][$cache_key];
try {
$cache_key = $this->getCacheKey($args);
if ($this->checkMethodCache($method, $args)) {
return $this->cache[$method][$cache_key];
}
} catch (\Exception $e) {
return false;
}

return false;
}

Expand All @@ -40,11 +49,15 @@ public function readMethodCache($method, array $args) {
* @return bool
*/
public function checkMethodCache($method, array $args) {
$cache_key = $this->getCacheKey($args);
if (!array_key_exists($method, $this->cache)) {
return false;
}
if (!array_key_exists($cache_key, $this->cache[$method])) {
try {
$cache_key = $this->getCacheKey($args);
if (!array_key_exists($method, $this->cache)) {
return false;
}
if (!array_key_exists($cache_key, $this->cache[$method])) {
return false;
}
} catch (\Exception $e) {
return false;
}
return true;
Expand Down