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->default_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( $callback, 'get_lag_cache' );

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

$wpdb->lag_cache_ttl = 30;
$wpdb->shmem_key     = ftok( __FILE__, "Y" );
$wpdb->shmem_size    = 128 * 1024;

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

function get_lag_cache( $wpdb ) {
    $segment  = shm_attach( $wpdb->shmem_key, $wpdb->shmem_size, 0600 );
    $lag_data = @shm_get_var( $segment, 0 );

    shm_detach( $segment );

    if ( ! is_array( $lag_data ) || !is_array( $lag_data[ $wpdb->lag_cache_key ] ) ) {
        return false;
    }

    if ( $wpdb->lag_cache_ttl < time() - $lag_data[ $wpdb->lag_cache_key ][ 'timestamp' ] ) {
        return false;
    }

    return $lag_data[ $wpdb->lag_cache_key ][ 'lag' ];
}

function get_lag( $wpdb ) {
    $dbh = $wpdb->dbhs[ $wpdb->dbhname ];

    if ( ! mysql_select_db( 'heartbeat', $dbh ) ) {
        return false;
    }

    $result = mysql_query( "SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(ts) AS lag FROM heartbeat LIMIT 1", $dbh );

    if ( ! $result || false === $row = mysql_fetch_assoc( $result ) ) {
        return false;
    }

    // Cache the result in shared memory with timestamp
    $sem_id = sem_get( $wpdb->shmem_key, 1, 0600, 1 );
    sem_acquire( $sem_id );
    $segment = shm_attach( $wpdb->shmem_key, $wpdb->shmem_size, 0600 );
    $lag_data = @shm_get_var( $segment, 0 );

    if ( ! is_array( $lag_data ) ) {
        $lag_data = array();
    }

    $lag_data[ $wpdb->lag_cache_key ] = array( 'timestamp' => time(), 'lag' => $row[ 'lag' ] );

    shm_put_var( $segment, 0, $lag_data );
    shm_detach( $segment );
    sem_release( $sem_id );

    return $row[ 'lag' ];
}