20 use Psr\Log\LoggerInterface;
21 use Psr\Log\LoggerAwareInterface;
50 const AUTH_NO_ERROR = 200;
57 const AUTH_ERROR_TEMPORARY = 201;
64 const AUTH_ERROR_PERMANENT = 202;
98 $this->lastError =
null;
111 $lname = strtolower(
$name );
113 ( $lname ===
'blpop' || $lname ===
'brpop' || $lname ===
'brpoplpush' )
114 &&
count( $arguments ) > 1
117 $timeout = end( $arguments );
119 $this->pool->resetTimeout( $this->conn, ( $timeout > 0 ? $timeout + 1 : $timeout ) );
133 private function tryCall( $method, $arguments ) {
134 $this->conn->clearLastError();
136 $res = $this->conn->$method( ...$arguments );
138 if ( $authError === self::AUTH_ERROR_TEMPORARY ) {
139 $res = $this->conn->$method( ...$arguments );
141 if ( $authError === self::AUTH_ERROR_PERMANENT ) {
142 throw new RedisException(
"Failure reauthenticating to Redis." );
161 public function scan( &$iterator, $pattern =
null, $count =
null ) {
162 return $this->
tryCall(
'scan', [ &$iterator, $pattern, $count ] );
176 public function sScan( $key, &$iterator, $pattern =
null, $count =
null ) {
177 return $this->
tryCall(
'sScan', [ $key, &$iterator, $pattern, $count ] );
191 public function hScan( $key, &$iterator, $pattern =
null, $count =
null ) {
192 return $this->
tryCall(
'hScan', [ $key, &$iterator, $pattern, $count ] );
206 public function zScan( $key, &$iterator, $pattern =
null, $count =
null ) {
207 return $this->
tryCall(
'zScan', [ $key, &$iterator, $pattern, $count ] );
216 if ( preg_match(
'/^ERR operation not permitted\b/', $this->conn->getLastError() ) ) {
217 if ( !$this->pool->reauthenticateConnection( $this->server, $this->conn ) ) {
218 return self::AUTH_ERROR_PERMANENT;
220 $this->conn->clearLastError();
222 "Used automatic re-authentication for Redis.",
223 [
'redis_server' => $this->
server ]
225 return self::AUTH_ERROR_TEMPORARY;
227 return self::AUTH_NO_ERROR;
239 $this->pool->resetTimeout( $this->conn );
250 $sha1 = sha1( $script );
255 $conn->clearLastError();
261 if ( preg_match(
'/^ERR operation not permitted\b/',
$conn->getLastError() ) ) {
262 $this->pool->reauthenticateConnection(
$server,
$conn );
263 $conn->clearLastError();
266 "Used automatic re-authentication for Lua script '$sha1'.",
271 if ( preg_match(
'/^NOSCRIPT/',
$conn->getLastError() ) ) {
272 $conn->clearLastError();
275 "Used eval() for Lua script '$sha1'.",
280 if (
$conn->getLastError() ) {
281 $this->logger->error(
282 'Lua script error on server "{redis_server}": {lua_error}',
285 'lua_error' =>
$conn->getLastError()
300 return $this->conn ===
$conn;
304 $this->pool->freeConnection( $this->
server, $this->conn );