20use Psr\Log\LoggerInterface;
21use 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;
109 public function __call( $name, $arguments ) {
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 ) );
122 return $this->
tryCall( $name, $arguments );
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 );
Helper class to handle automatically marking connectons as reusable (via RAII pattern)
RedisConnectionPool $pool
zScan( $key, &$iterator, $pattern=null, $count=null)
Sorted Set Scan Handle this explicity due to needing the iterator passed by reference.
scan(&$iterator, $pattern=null, $count=null)
Key Scan Handle this explicity due to needing the iterator passed by reference.
tryCall( $method, $arguments)
Do the method call in the common try catch handler.
__call( $name, $arguments)
Magic __call handler for most Redis functions.
luaEval( $script, array $params, $numKeys)
sScan( $key, &$iterator, $pattern=null, $count=null)
Set Scan Handle this explicity due to needing the iterator passed by reference.
hScan( $key, &$iterator, $pattern=null, $count=null)
Hash Scan Handle this explicity due to needing the iterator passed by reference.
__construct(RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger)
setLogger(LoggerInterface $logger)
postCallCleanup()
Post Redis call cleanup.
checkAuthentication()
Handle authentication errors and automatically reauthenticate.
isConnIdentical(Redis $conn)
Helper class to manage Redis connections.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))