Skip to content

Commit

Permalink
Predis with cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
yatsukhnenko committed Mar 13, 2024
1 parent 0f2aa78 commit 639b3a2
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions includes/object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -1600,17 +1600,32 @@ protected function execute_lua_script( $script ) {

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 );
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);
}
}
} catch ( Exception $exception ) {
$this->handle_exception( $exception );
$this->redis = $redis;
} 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;
return false;
}
}
$this->redis = $redis;
} else {
Expand Down Expand Up @@ -1663,15 +1678,21 @@ public function flush() {

if ( $salt && $selective ) {
$script = $this->get_flush_closure( $salt );
$results = $this->execute_lua_script( $script() );
$results = $this->execute_lua_script( $script );
if ( empty( $results ) ) {
return false;
}
} else {
if ( defined( 'WP_REDIS_CLUSTER' ) ) {
try {
foreach ( $this->redis->_masters() as $master ) {
$results[] = $this->parse_redis_response( $this->redis->flushdb( $master ) );
if ( $this->is_predis() ) {
foreach ( $this->redis->getIterator() as $master ) {
$results[] = $this->parse_redis_response( $master->flushdb() );
}
} else {
foreach ( $this->redis->_masters() as $master ) {
$results[] = $this->parse_redis_response( $this->redis->flushdb( $master ) );
}
}
} catch ( Exception $exception ) {
$this->handle_exception( $exception );
Expand Down Expand Up @@ -1751,7 +1772,7 @@ public function flush_group( $group )

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

if ( empty( $results ) ) {
return false;
Expand Down

0 comments on commit 639b3a2

Please sign in to comment.