Last active
April 1, 2021 02:56
-
-
Save harisrozak/ba096733f99b1e3ff394bd58ba058dbc to your computer and use it in GitHub Desktop.
LudicrousDB (based on Automattic's HyperDB) configuration file that will split multisite per 100 sites based on site ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* LudicrousDB configuration file | |
* | |
* This file should be copied to ABSPATH/db-config.php and modified to suit your | |
* database environment. This file comes with a basic configuration by default. | |
* | |
* See README.md for documentation. | |
* | |
* LudicrousDB on Github: https://github.com/stuttter/ludicrousdb | |
*/ | |
// Exit if accessed directly | |
defined( 'ABSPATH' ) || exit; | |
/** | |
* charset (string) | |
* This sets the default character set. Since WordPress 4.2, the suggested | |
* setting is "utf8mb4". We strongly recommend not downgrading to utf8, | |
* using latin1, or sticking to the default: utf8mb4. | |
* | |
* Default: utf8mb4 | |
*/ | |
$wpdb->charset = 'utf8mb4'; | |
/** | |
* collate (string) | |
* This sets the default column collation. For best results, investigate which | |
* collation is recommended for your specific character set. | |
* | |
* Default: utf8mb4_unicode_520_ci | |
*/ | |
$wpdb->collate = 'utf8mb4_unicode_520_ci'; | |
/** | |
* save_queries (bool) | |
* This is useful for debugging. Queries are saved in $wpdb->queries. It is not | |
* a constant because you might want to use it momentarily. | |
* Default: false | |
*/ | |
$wpdb->save_queries = false; | |
/** | |
* recheck_timeout (float) | |
* The amount of time to wait before trying again to ping mysql server. | |
* | |
* Default: 0.1 (Seconds) | |
*/ | |
$wpdb->recheck_timeout = 0.1; | |
/** | |
* persistent (bool) | |
* This determines whether to use mysql_connect or mysql_pconnect. The effects | |
* of this setting may vary and should be carefully tested. | |
* Default: false | |
*/ | |
$wpdb->persistent = false; | |
/** | |
* allow_bail (bool) | |
* This determines whether to use mysql connect or mysql connect has failed and to bail loading the rest of WordPress | |
* Default: false | |
*/ | |
$wpdb->allow_bail = false; | |
/** | |
* max_connections (int) | |
* This is the number of mysql connections to keep open. Increase if you expect | |
* to reuse a lot of connections to different servers. This is ignored if you | |
* enable persistent connections. | |
* Default: 10 | |
*/ | |
$wpdb->max_connections = 10; | |
/** | |
* check_tcp_responsiveness | |
* Enables checking TCP responsiveness by fsockopen prior to mysql_connect or | |
* mysql_pconnect. This was added because PHP's mysql functions do not provide | |
* a variable timeout setting. Disabling it may improve average performance by | |
* a very tiny margin but lose protection against connections failing slowly. | |
* Default: true | |
*/ | |
$wpdb->check_tcp_responsiveness = true; | |
/** | |
* The cache group that is used to store TCP responsiveness. | |
* Default: ludicrousdb | |
*/ | |
$wpdb->cache_group = 'ludicrousdb'; | |
/** | |
* $callback is a callable function or method. It will be called with two | |
* arguments and expected to compute a dataset or return null. | |
* $dataset = $callback($table, &$wpdb); | |
* | |
* Callbacks are executed in the order in which they are registered until one | |
* of them returns something other than null. Anything evaluating to false will | |
* cause the query to be aborted. | |
*/ | |
$wpdb->add_callback( 'commercioo_saas_resolve_with_dataset' ); | |
/** | |
* Resolve the database name to use. | |
* This will return a dataset string between `sites_100` to `sites_9999000` | |
* | |
* @param object $query | |
* @param object $wpdb | |
* @return string $dataset | |
*/ | |
function commercioo_saas_resolve_with_dataset( $query, $wpdb ) { | |
$dataset = null; | |
$prefix_matches = array(); | |
$site_id_matches = array(); | |
// Multisite blog tables are "{$base_prefix}{$blog_id}_*" | |
if ( preg_match("/^{$wpdb->base_prefix}\d+_/i", $wpdb->table) ) { | |
// break down into just wp_86_ prefix | |
preg_match("/^{$wpdb->base_prefix}\d+_/i", $wpdb->table, $prefix_matches); | |
$site_table_prefix = $prefix_matches[0]; | |
// pull out any numerical matches here | |
preg_match("/\d+/i", $site_table_prefix, $site_id_matches); | |
$raw_site_id = $site_id_matches[0]; | |
$site_id = ( int ) $raw_site_id; | |
$previous_limit = 0; | |
/** | |
* Iterate per 100 | |
* The limit is 9999000, so it 10 million minus 1 sites | |
*/ | |
for ( $i = 1; $i < 9999; $i++ ) { | |
$sites_limit = $i * 100; | |
if ( $site_id > $previous_limit && $site_id <= $sites_limit ) { | |
$dataset = 'sites_' . $sites_limit; | |
break; | |
} | |
// set previous limit | |
$previous_limit = $sites_limit; | |
} | |
} | |
return $dataset; | |
} | |
/** | |
* This is a multisite per 100 sites setting, that using iterate per 100 sites per database | |
* The main site will be on the default database, then the child sites will be on `sites_100`, `sites_200`, etc dataset | |
* This database sets is expandable, so it can be only 2 databases at first and then we can continue add to it later when it's needed | |
*/ | |
$wpdb->add_database( array( | |
'host' => sprintf( "%s:%d", DB_HOST, 10053 ), // host:port | |
'user' => DB_USER, | |
'password' => DB_PASSWORD, | |
'name' => DB_NAME, | |
'write' => 1, | |
'read' => 1, | |
'dataset' => 'global' | |
) ); | |
$wpdb->add_database( array( | |
'host' => sprintf( "%s:%d", DB_HOST, 10053 ), // host:port | |
'user' => DB_USER, | |
'password' => DB_PASSWORD, | |
'name' => 'comm_sites_100', | |
'write' => 1, | |
'read' => 1, | |
'dataset' => 'sites_100' | |
) ); | |
$wpdb->add_database( array( | |
'host' => sprintf( "%s:%d", DB_HOST, 10053 ), // host:port | |
'user' => DB_USER, | |
'password' => DB_PASSWORD, | |
'name' => 'comm_sites_200', | |
'write' => 1, | |
'read' => 1, | |
'dataset' => 'sites_200', | |
) ); | |
$wpdb->add_database( array( | |
'host' => sprintf( "%s:%d", DB_HOST, 10053 ), // host:port | |
'user' => DB_USER, | |
'password' => DB_PASSWORD, | |
'name' => 'comm_sites_300', | |
'write' => 1, | |
'read' => 1, | |
'dataset' => 'sites_300', | |
) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment