Created
May 15, 2023 14:42
-
-
Save linuxd3v/43287b4562b36c6e6ba5872b7c07e265 to your computer and use it in GitHub Desktop.
PHP Swoole - properly returning the pooled redis handle into the pool in a "finally" block
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
protected function removeSomeListItemBlahBlah(string $name, string $key) | |
{ | |
try { | |
$cacheHandle = $this->pool->borrow(); | |
$result = $cacheHandle->lrem($name, $key, 0); | |
} catch (Throwable $e) { | |
// Log the error | |
$this->logger->error('Exception while trying to removeSomeListItemBlahBlah', ['exception' => $e]); | |
// Rethrow exception as we are unable to gracefully recover | |
throw $e; | |
// Remember - finally will ALWAYS get executed first, even if u re-throw exception in catch block. | |
} finally { | |
$this->pool->return($cacheHandle); | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment