Skip to content

Commit

Permalink
Add execute_lua_script helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
yatsukhnenko committed Mar 11, 2024
1 parent 704ac36 commit bf48993
Showing 1 changed file with 62 additions and 63 deletions.
125 changes: 62 additions & 63 deletions includes/object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,61 @@ public function flush_runtime() {
return true;
}

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

if ( defined( 'WP_REDIS_CLUSTER' ) ) {
$redis = $this->redis;
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;
}
$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);
}
}

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);
}
}
}

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 All @@ -1608,28 +1663,9 @@ public function flush() {

if ( $salt && $selective ) {
$script = $this->get_flush_closure( $salt );

if ( defined( 'WP_REDIS_CLUSTER' ) ) {
try {
foreach ( $this->redis->_masters() as $master ) {
$redis = new Redis();
$redis->connect( $master[0], $master[1] );
$results[] = $this->parse_redis_response( $script() );
unset( $redis );
}
} catch ( Exception $exception ) {
$this->handle_exception( $exception );

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

return false;
}
$results = $this->execute_lua_script( $script() );
if ( empty( $results ) ) {
return false;
}
} else {
if ( defined( 'WP_REDIS_CLUSTER' ) ) {
Expand Down Expand Up @@ -1713,25 +1749,11 @@ public function flush_group( $group )
return false;
}

$results = [];

$start_time = microtime( true );
$script = $this->lua_flush_closure( $salt, false );
$results = $this->execute_lua_script( $script() );

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

if ( empty( $results ) ) {
return false;
}

Expand Down Expand Up @@ -1825,32 +1847,9 @@ protected function lua_flush_closure( $salt, $escape = true ) {
$script = 'redis.replicate_commands()' . "\n" . $script;
}

if ( $this->is_predis() ) {
$args = [ $script, 0 ];
if ( defined('WP_REDIS_FLUSH_TIMEOUT') ) {
$timeout = $this->redis->getConnection()->getParameters()->read_write_timeout ?? ini_get( 'default_socket_timeout' );
// $timeout = stream_context_get_options($this->redis->getConnection()->getResource())['socket']['timeout'] ?? ini_get( 'default_socket_timeout' );
stream_set_timeout($this->redis->getConnection()->getResource(), WP_REDIS_FLUSH_TIMEOUT);
}
} else {
$args = [ $script ];
if ( defined('WP_REDIS_FLUSH_TIMEOUT') ) {
$timeout = $this->redis->getOption(Redis::OPT_READ_TIMEOUT);
$this->redis->setOption(Redis::OPT_READ_TIMEOUT, WP_REDIS_FLUSH_TIMEOUT);
}
}

$result = call_user_func_array( [ $this->redis, 'eval' ], $args );

if ( isset($timeout) ) {
if ( $this->is_predis() ) {
stream_set_timeout($this->redis->getConnection()->getResource(), $timeout);
} else {
$this->redis->setOption(Redis::OPT_READ_TIMEOUT, $timeout);
}
}
$args = $this->is_predis() ? [ $script, 0 ] : [ $script ];

return $result;
return call_user_func_array( [ $this->redis, 'eval' ], $args );
};
}

Expand Down

0 comments on commit bf48993

Please sign in to comment.