Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save linuxd3v/43287b4562b36c6e6ba5872b7c07e265 to your computer and use it in GitHub Desktop.
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
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