Last active
June 25, 2018 14:34
-
-
Save masato-igarashi/ce92c71cecd93cab1ebf964adc72c0ba to your computer and use it in GitHub Desktop.
hyperdbでDBをレプリケーションした際、レプリカラグによるwoocommerce ajaxカート不具合の解消
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 | |
$wpdb->save_queries = false; | |
$wpdb->persistent = false; | |
$wpdb->max_connections = 0; | |
$wpdb->check_tcp_responsiveness = true; | |
//マスターDB設定 | |
//readはスレーブがタイムアウトしたときのために2に設定 | |
$wpdb->add_database(array( | |
'host' => DB_HOST, | |
'user' => DB_USER, | |
'password' => DB_PASSWORD, | |
'name' => DB_NAME, | |
'write' => 1, | |
'read' => 2, | |
'dataset' => 'global' | |
)); | |
//マスターDBと同一情報のDB接続先を'dataset' => 'sessions'で定義 | |
$wpdb->add_database(array( | |
'host' => DB_HOST, | |
'user' => DB_USER, | |
'password' => DB_PASSWORD, | |
'name' => DB_NAME, | |
'write' => 1, | |
'read' => 2, | |
'dataset' => 'sessions' | |
)); | |
//スレーブDB(リードレプリカ)設定 | |
//DB_HOST_READERはwp-config.phpで定義。 | |
//db-config.phpには接続先情報を直接記述しない。 | |
//example: | |
//define('DB_HOST', 'example.com'); | |
//define('DB_HOST_READER', 'reader.example.com'); | |
$wpdb->add_database(array( | |
'host' => DB_HOST_READER, | |
'user' => DB_USER, | |
'password' => DB_PASSWORD, | |
'name' => DB_NAME, | |
'write' => 0, | |
'read' => 1, | |
'dataset' => 'global', | |
'timeout' => 0.5 | |
)); | |
//レプリカラグによるwoocommerceカート不具合の解消 | |
//woocommerce_sessionsのクエリはすべてマスターDBへ | |
$wpdb->add_callback('for_woocommerce_sessions'); | |
function for_woocommerce_sessions($query, $wpdb) { | |
if ( $wpdb->base_prefix . 'woocommerce_sessions' == $wpdb->table ) { | |
return 'sessions'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment