Skip to content

Commit

Permalink
Merge branch 'issue/102' into 'master'
Browse files Browse the repository at this point in the history
Issue/102

See merge request publicious.org/plugins/ludicrousdb!18
  • Loading branch information
JJJ committed Oct 26, 2020
2 parents 8217a62 + 548695b commit 3bd9160
Showing 1 changed file with 91 additions and 25 deletions.
116 changes: 91 additions & 25 deletions ludicrousdb/includes/class-ludicrousdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,13 +740,13 @@ public function db_connect( $query = '' ) {

$this->timer_start();

// Maybe check TCP responsiveness
$tcp = ! empty( $this->check_tcp_responsiveness )
? $this->check_tcp_responsiveness( $host, $port, $timeout )
: null;

// Connect if necessary or possible
$tcp = null;
if ( ! empty( $use_master )
|| empty( $tries_remaining )
|| empty( $this->check_tcp_responsiveness )
|| ( true === $tcp = $this->check_tcp_responsiveness( $host, $port, $timeout ) )
) {
if ( ! empty( $use_master ) || empty( $tries_remaining ) || ( true === $tcp ) ) {
$this->single_db_connect( $dbhname, $host_and_port, $user, $password );
} else {
$this->dbhs[ $dbhname ] = false;
Expand Down Expand Up @@ -2133,9 +2133,33 @@ protected function tcp_get_cache_expiration() {
return 10;
}

/**
* Check if TCP is using a persistent cache or not.
*
* @since 5.1.0
*
* @return bool True if yes. False if no.
*/
protected function tcp_is_cache_persistent() {

// Check if using external object cache
if ( wp_using_ext_object_cache() ) {

// Make sure the global group is added
$this->add_global_group();

// Yes
return true;
}

// No
return false;
}

/**
* Get cached up/down value of previous TCP response.
* Look in local cache in case external cache isn't in use or not ready yet.
*
* Falls back to local cache if persistent cache is not available.
*
* @since 3.0.0
*
Expand All @@ -2150,27 +2174,34 @@ protected function tcp_cache_get( $key = '' ) {
return false;
}

// Return cache value if set
if ( isset( $this->tcp_cache[ $key ] ) ) {
return $this->tcp_cache[ $key ];
}
// Get from persistent cache
if ( $this->tcp_is_cache_persistent() ) {
return wp_cache_get( $key, $this->cache_group );

// Maybe get from persistent cache
if ( wp_using_ext_object_cache() ) {
$this->add_global_group();
// Fallback to local cache
} elseif ( ! empty( $this->tcp_cache[ $key ] ) ) {

// Not expired
if ( ! empty( $this->tcp_cache[ $key ]['expiration'] ) && ( time() < $this->tcp_cache[ $key ]['expiration'] ) ) {

// Set value from persistent
$this->tcp_cache[ $key ] = wp_cache_get( $key, $this->cache_group );
// Return value or false if empty
return ! empty( $this->tcp_cache[ $key ]['value'] )
? $this->tcp_cache[ $key ]['value']
: false;

return $this->tcp_cache[ $key ];
// Expired, so delete and proceed
} else {
$this->tcp_cache_delete( $key );
}
}

return false;
}

/**
* Set cached up/down value of current TCP response.
* Store in local cache in case external cache isn't in use or not ready yet.
*
* Falls back to local cache if persistent cache is not available.
*
* @since 3.0.0
*
Expand All @@ -2183,17 +2214,52 @@ protected function tcp_cache_set( $key = '', $value = '' ) {

// Bail if invalid values were passed
if ( empty( $key ) || empty( $value ) ) {
return true;
return false;
}

// Add value to cache
$this->tcp_cache[ $key ] = $value;
// Get expiration
$expires = $this->tcp_get_cache_expiration();

// Maybe add to persistent cache
if ( wp_using_ext_object_cache() ) {
$this->add_global_group();
// Add to persistent cache
if ( $this->tcp_is_cache_persistent() ) {
return wp_cache_set( $key, $value, $this->cache_group, $expires );

// Fallback to local cache
} else {
$this->tcp_cache[ $key ] = array(
'value' => $value,
'expiration' => time() + $expires
);
}

return true;
}

/**
* Delete cached up/down value of current TCP response.
*
* Falls back to local cache if persistent cache is not available.
*
* @since 5.1.0
*
* @param string $key Results of tcp_get_cache_key()
*
* @return bool Results of wp_cache_delete() or true
*/
protected function tcp_cache_delete( $key = '' ) {

return wp_cache_set( $key, $value, $this->cache_group, $this->tcp_get_cache_expiration() );
// Bail if invalid key
if ( empty( $key ) ) {
return false;
}

// Delete from persistent cache
if ( $this->tcp_is_cache_persistent() ) {
return wp_cache_delete( $key, $this->cache_group );

// Fallback to local cache
} else {
unset( $this->tcp_cache[ $key ] );
}

return true;
Expand Down

0 comments on commit 3bd9160

Please # to comment.