From 2da95e280d3019bdcfbc8b2286ef7d561d9bd656 Mon Sep 17 00:00:00 2001
From: Erick Hitter <ehitter@gmail.com>
Date: Wed, 26 Feb 2014 18:22:45 -0800
Subject: [PATCH] Only set database value when not zero, otherwise unnecessary
 `SELECT` calls result

---
 index-wp-redis.php                | 23 ++++++++++++++++-------
 wp-redis-cache/wp-redis-cache.php |  7 +++----
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/index-wp-redis.php b/index-wp-redis.php
index e0e5780..6078c12 100644
--- a/index-wp-redis.php
+++ b/index-wp-redis.php
@@ -196,9 +196,12 @@ function wp_redis_cache_connect_redis() {
 		}
 
 		$redis = new Redis();
-
 		$redis->connect( $wp_redis_cache_config['redis_server'], $wp_redis_cache_config['redis_port'] );
-		$redis->select( $wp_redis_cache_config['redis_db'] );
+
+		// Default DB is 0, so only need to SELECT if other
+		if ( $wp_redis_cache_config['redis_db'] ) {
+			$redis->select( $wp_redis_cache_config['redis_db'] );
+		}
 	// Fallback to predis5.2.php
 	} else {
 		if ( $wp_redis_cache_config['debug'] ) {
@@ -206,11 +209,17 @@ function wp_redis_cache_connect_redis() {
 		}
 
 		include_once dirname( __FILE__ ) . '/wp-content/plugins/wp-redis-cache/predis5.2.php'; //we need this to use Redis inside of PHP
-		$redis = new Predis_Client( array(
-			'host'     => $wp_redis_cache_config['redis_server'],
-			'port'     => $wp_redis_cache_config['redis_port'],
-			'database' => $wp_redis_cache_config['redis_db'],
-		) );
+		$redis = array(
+			'host' => $wp_redis_cache_config['redis_server'],
+			'port' => $wp_redis_cache_config['redis_port'],
+		);
+
+		// Default DB is 0, so only need to SELECT if other
+		if ( $wp_redis_cache_config['redis_db'] ) {
+			$redis['database'] = $wp_redis_cache_config['redis_db'];
+		}
+
+		$redis = new Predis_Client( $redis );
 	}
 
 	return $redis;
diff --git a/wp-redis-cache/wp-redis-cache.php b/wp-redis-cache/wp-redis-cache.php
index 4da7a70..ffb214d 100644
--- a/wp-redis-cache/wp-redis-cache.php
+++ b/wp-redis-cache/wp-redis-cache.php
@@ -134,17 +134,16 @@ class WP_Redis_Cache {
 			$redis_settings = array(
 				'host'     => '127.0.0.1',
 				'port'     => 6379,
-				'database' => 0,
 			);
 
 			// Override default connection settings with global values, when present
-			if ( defined( 'WP_REDIS_CACHE_REDIS_HOST' ) ) {
+			if ( defined( 'WP_REDIS_CACHE_REDIS_HOST' ) && WP_REDIS_CACHE_REDIS_HOST ) {
 				$redis_settings['host'] = WP_REDIS_CACHE_REDIS_HOST;
 			}
-			if ( defined( 'WP_REDIS_CACHE_REDIS_PORT' ) ) {
+			if ( defined( 'WP_REDIS_CACHE_REDIS_PORT' ) && WP_REDIS_CACHE_REDIS_PORT ) {
 				$redis_settings['port'] = WP_REDIS_CACHE_REDIS_PORT;
 			}
-			if ( defined( 'WP_REDIS_CACHE_REDIS_DB' ) ) {
+			if ( defined( 'WP_REDIS_CACHE_REDIS_DB' ) && WP_REDIS_CACHE_REDIS_DB ) {
 				$redis_settings['database'] = WP_REDIS_CACHE_REDIS_DB;
 			}
 
-- 
GitLab