diff --git a/ludicrousdb/includes/class-ludicrousdb.php b/ludicrousdb/includes/class-ludicrousdb.php index 8f7bea9..55b1146 100644 --- a/ludicrousdb/includes/class-ludicrousdb.php +++ b/ludicrousdb/includes/class-ludicrousdb.php @@ -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; @@ -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 * @@ -2150,19 +2174,25 @@ 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; @@ -2170,7 +2200,8 @@ protected function tcp_cache_get( $key = '' ) { /** * 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 * @@ -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;