PHP code example of stuttter / ludicrousdb

1. Go to this page and download the library: Download stuttter/ludicrousdb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stuttter / ludicrousdb example snippets


$wpdb->add_database( array(
    'host'     => DB_HOST,     // If port is other than 3306, use host:port.
    'user'     => DB_USER,
    'password' => DB_PASSWORD,
    'name'     => DB_NAME,
) );

$wpdb->add_database( array(
    'host'     => DB_HOST,     // If port is other than 3306, use host:port.
    'user'     => DB_USER,
    'password' => DB_PASSWORD,
    'name'     => DB_NAME,
    'write'    => 0,
    'read'     => 1,
    'dataset'  => 'global',
    'timeout'  => 0.2,
) );

$wpdb->add_database( array(
    'host'     => 'global.db.example.com',
    'user'     => 'globaluser',
    'password' => 'globalpassword',
    'name'     => 'globaldb',
) );

$wpdb->add_database( array(
    'host'     => 'blog.db.example.com',
    'user'     => 'bloguser',
    'password' => 'blogpassword',
    'name'     => 'blogdb',
    'dataset'  => 'blog',
) );

$wpdb->add_callback( 'my_db_callback' );

// Multisite blog tables are "{$base_prefix}{$blog_id}_*"
function my_db_callback( $query, $wpdb ) {
    if ( preg_match("/^{$wpdb->base_prefix}\d+_/i", $wpdb->table) ) {
        return 'blog';
    }
}

$wpdb->add_database( $database );

host          ((optional) Whether server is readable. Default is 1 (readable).
                         Also used to assign preference. See "Network topology".
write         (optional) Whether server is writable. Default is 1 (writable).
                         Also used to assign preference in multi-primary mode.
dataset       (optional) Name of dataset. Default is 'global'.
timeout       (optional) Seconds to wait for TCP responsiveness. Default is 0.2
lag_threshold (optional) The minimum lag on a replica in seconds before we consider it lagged.
                         Set null to disable. When not set, the value of
                         $wpdb->database_defaults['lag_threshold'] is used.

$wpdb->add_table( $dataset, $table );

$wpdb->add_callback( $callback, $callback_group = 'dataset' );

$dataset = $callback($table, &$wpdb);

'write' => 1,
'read'  => 1,

'write' => 0,
'read'  => 1,

'write' => 1,
'read'  => 0,

Local replica 1:   'write' => 0, 'read' => 1,
Local replica 2:   'write' => 0, 'read' => 1,
Local primary:     'write' => 1, 'read' => 2,
Remote replica 1:  'write' => 0, 'read' => 3,
Remote replica 2:  'write' => 0, 'read' => 3,

Local replica 1:   'write' => 0, 'read' => 1,
Local replica 2:   'write' => 0, 'read' => 1,
Remote replica 1:  'write' => 0, 'read' => 2,
Remote replica 2:  'write' => 0, 'read' => 2,
Remote primary:    'write' => 1, 'read' => 3,

$wpdb->add_callback( 'ludicrousdb_get_lag_cache', 'get_lag_cache' );

$wpdb->add_callback( 'ludicrousdb_get_lag', 'get_lag' );

if ( ! defined( 'LUDICROUSDB_LAG_CACHE_TTL' ) ) {
    define( 'LUDICROUSDB_LAG_CACHE_TTL', 30 );
}

$wpdb->add_callback( 'ludicrousdb_get_lag_cache', 'get_lag_cache' );
$wpdb->add_callback( 'ludicrousdb_get_lag', 'get_lag' );

function ludicrousdb_get_lag_cache( $wpdb ) {
    if ( ! function_exists( 'wp_cache_get' ) || empty( $GLOBALS['wp_object_cache'] ) ) {
        return false;
    }

    $lag = wp_cache_get( $wpdb->lag_cache_key, 'ludicrousdb-lag' );

    return is_numeric( $lag ) ? (float) $lag : false;
}

function ludicrousdb_get_lag( $wpdb ) {
    if ( empty( $wpdb->dbhs[ $wpdb->dbhname ] ) ) {
        return false;
    }

    $dbh    = $wpdb->dbhs[ $wpdb->dbhname ];
    $result = mysqli_query(
        $dbh,
        'SELECT GREATEST( 0, UNIX_TIMESTAMP() - UNIX_TIMESTAMP( ts ) ) AS lag
        FROM heartbeat.heartbeat
        ORDER BY ts DESC
        LIMIT 1'
    );

    if ( false === $result ) {
        return false;
    }

    $row = mysqli_fetch_assoc( $result );
    mysqli_free_result( $result );

    if ( ! is_array( $row ) || ! isset( $row['lag'] ) || ! is_numeric( $row['lag'] ) ) {
        return false;
    }

    $lag = (float) $row['lag'];

    if ( function_exists( 'wp_cache_set' ) && ! empty( $GLOBALS['wp_object_cache'] ) ) {
        wp_cache_set( $wpdb->lag_cache_key, $lag, 'ludicrousdb-lag', LUDICROUSDB_LAG_CACHE_TTL );
    }

    return $lag;
}