Skip to content

Commit

Permalink
split up functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tillkruss committed Mar 18, 2024
1 parent 639b3a2 commit c2dcf02
Showing 1 changed file with 60 additions and 44 deletions.
104 changes: 60 additions & 44 deletions includes/object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -1599,67 +1599,83 @@ protected function execute_lua_script( $script ) {
$results = [];

if ( defined( 'WP_REDIS_CLUSTER' ) ) {
$redis = $this->redis;
return $this->execute_lua_script_on_cluster( $script )
}

if ( defined( 'WP_REDIS_FLUSH_TIMEOUT' ) ) {
if ( $this->is_predis() ) {
foreach ( $this->redis->getIterator() as $master ) {
if ( defined( 'WP_REDIS_FLUSH_TIMEOUT' ) ) {
$timeout = $master->getConnection()->getParameters()->read_write_timeout ?? ini_get( 'default_socket_timeout' );
stream_set_timeout($master->getConnection()->getResource(), WP_REDIS_FLUSH_TIMEOUT);
}
$this->redis = $master;
$results[] = $this->parse_redis_response( $script() );
if ( isset($timeout) ) {
stream_set_timeout($master->getConnection()->getResource(), $timeout);
unset($timeout);
}
}
$timeout = $this->redis->getConnection()->getParameters()->read_write_timeout ?? ini_get( 'default_socket_timeout' );
stream_set_timeout( $this->redis->getConnection()->getResource(), WP_REDIS_FLUSH_TIMEOUT );
} else {
try {
foreach ( $this->redis->_masters() as $master ) {
$this->redis = new Redis();
$this->redis->connect( $master[0], $master[1], defined( 'WP_REDIS_FLUSH_TIMEOUT' ) ? WP_REDIS_FLUSH_TIMEOUT : 0 );
$results[] = $this->parse_redis_response( $script() );
}
} catch ( Exception $exception ) {
$this->handle_exception( $exception );
$this->redis = $redis;
$timeout = $this->redis->getOption( Redis::OPT_READ_TIMEOUT );
$this->redis->setOption( Redis::OPT_READ_TIMEOUT, WP_REDIS_FLUSH_TIMEOUT );
}
}

return false;
}
try {
$results[] = $this->parse_redis_response( $script() );
} catch ( Exception $exception ) {
$this->handle_exception( $exception );

return false;
}

if ( isset( $timeout ) ) {
if ( $this->is_predis() ) {
stream_set_timeout( $this->redis->getConnection()->getResource(), $timeout );
} else {
$this->redis->setOption( Redis::OPT_READ_TIMEOUT, $timeout );
}
$this->redis = $redis;
} else {
if ( defined('WP_REDIS_FLUSH_TIMEOUT') ) {
if ( $this->is_predis() ) {
$timeout = $this->redis->getConnection()->getParameters()->read_write_timeout ?? ini_get( 'default_socket_timeout' );
stream_set_timeout($this->redis->getConnection()->getResource(), WP_REDIS_FLUSH_TIMEOUT);
} else {
$timeout = $this->redis->getOption(Redis::OPT_READ_TIMEOUT);
$this->redis->setOption(Redis::OPT_READ_TIMEOUT, WP_REDIS_FLUSH_TIMEOUT);
}

return $results;
}

/**
* ...
*
* @return array|false Returns array on success, false on failure
*/
protected function execute_lua_script_on_cluster( $script ) {
$results = [];

$redis = $this->redis;

if ( $this->is_predis() ) {
foreach ( $this->redis->getIterator() as $master ) {
if ( defined( 'WP_REDIS_FLUSH_TIMEOUT' ) ) {
$timeout = $master->getConnection()->getParameters()->read_write_timeout ?? ini_get( 'default_socket_timeout' );
stream_set_timeout( $master->getConnection()->getResource(), WP_REDIS_FLUSH_TIMEOUT );
}
}

try {
$this->redis = $master;
$results[] = $this->parse_redis_response( $script() );

if ( isset($timeout) ) {
stream_set_timeout($master->getConnection()->getResource(), $timeout);
unset($timeout);
}
}
} else {
try {
foreach ( $this->redis->_masters() as $master ) {
$this->redis = new Redis();
$this->redis->connect( $master[0], $master[1], defined( 'WP_REDIS_FLUSH_TIMEOUT' ) ? WP_REDIS_FLUSH_TIMEOUT : 0 );
$results[] = $this->parse_redis_response( $script() );
}
} catch ( Exception $exception ) {
$this->handle_exception( $exception );
$this->redis = $redis;

return false;
}

if ( isset($timeout) ) {
if ( $this->is_predis() ) {
stream_set_timeout($this->redis->getConnection()->getResource(), $timeout);
} else {
$this->redis->setOption(Redis::OPT_READ_TIMEOUT, $timeout);
}
}
}

$this->redis = $redis;

return $results;
}


/**
* Invalidate all items in the cache. If `WP_REDIS_SELECTIVE_FLUSH` is `true`,
* only keys prefixed with the `WP_REDIS_PREFIX` are flushed.
Expand Down

0 comments on commit c2dcf02

Please sign in to comment.