Note: 锟芥本锟斤拷锟斤拷锟斤拷锟斤拷锟皆猴拷状态
锟斤拷锟皆达拷 concepts section 锟斤拷取锟斤拷锟斤拷锟斤拷锟斤拷荨锟�
锟斤拷锟捷匡拷群锟斤拷锟斤拷锟斤拷瓒拷锟酵拷锟揭伙拷锟斤拷约锟斤拷稹4锟� 1.2.0 锟芥本锟斤拷锟斤拷锟角斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷群锟斤拷诘锟窖★拷锟斤拷锟� 锟斤拷锟斤拷使锟矫碉拷一锟斤拷锟皆硷拷锟斤拷锟斤拷锟界,锟斤拷锟绞癸拷锟斤拷觳斤拷锟� MySQL 锟斤拷锟斤拷同锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷群锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷裕锟� 锟斤拷锟斤拷锟斤拷锟轿猴拷时锟斤拷通锟斤拷 mysqlnd_ms_set_quos() 使锟斤拷 Session 一锟斤拷锟皆★拷 锟斤拷锟斤拷圆慰锟� 锟斤拷锟今级憋拷锟斤拷一锟斤拷锟斤拷
Example #1 Recap: quality of service to request read your writes
/* 锟借定锟斤拷锟斤拷 Session 一锟斤拷锟斤拷 */
if (!mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_SESSION))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
锟斤拷锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷一锟斤拷锟皆凤拷锟今级别,讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟揭伙拷锟斤拷圆锟斤拷浴锟斤拷锟矫达拷锟斤拷锟斤拷锟斤拷使锟斤拷 通锟斤拷 TTL 失效锟斤拷锟皆匡拷锟狡的伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷菘锟节碉拷锟饺★拷锟斤拷荨锟斤拷锟斤拷菘锟节碉拷突锟斤拷娑硷拷锟斤拷锟� 锟斤拷锟斤拷一锟斤拷锟皆诧拷锟皆o拷锟斤拷锟角匡拷锟杰憋拷锟斤拷牟锟斤拷锟斤拷锟斤拷碌锟斤拷锟斤拷荨锟�
通锟斤拷锟斤拷锟截伙拷锟斤拷锟斤拷锟斤拷锟斤拷菘锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷效锟斤拷锟斤拷锟斤拷锟斤拷锟杰o拷锟斤拷锟斤拷锟斤拷锟捷匡拷压锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷突锟斤拷吮却锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷目锟侥客伙拷锟剿革拷频锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷么锟斤拷锟捷匡拷姆锟斤拷示捅锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� 锟接讹拷锟斤拷锟斤拷锟斤拷锟捷匡拷压锟斤拷锟斤拷锟斤拷锟揭o拷锟斤拷锟节憋拷锟截伙拷锟斤拷锟斤拷俣瓤锟斤拷锟斤拷锟斤拷菘锟斤拷询锟斤拷锟斤拷么锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷芫捅锟斤拷锟斤拷锟斤拷锟�
Example #2 Plugin config: no special entries for caching
{ "myapp": { "master": { "master_0": { "host": "localhost", "socket": "\/tmp\/mysql.sock" } }, "slave": { "slave_0": { "host": "127.0.0.1", "port": "3306" } }, } }
Example #3 Caching a slave request
<?php
$mysqli = new mysqli("myapp", "username", "password", "database");
if (!$mysqli)
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
if (!$mysqli->query("DROP TABLE IF EXISTS test") ||
!$mysqli->query("CREATE TABLE test(id INT)") ||
!$mysqli->query("INSERT INTO test(id) VALUES (1)"))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* 确锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟皆o拷锟斤拷锟斤拷锟借定锟斤拷锟斤拷锟斤拷效锟斤拷 (TTL <= 60 seconds) */
if (false == mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL, MYSQLND_MS_QOS_OPTION_CACHE, 60)) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
/* 为锟斤拷锟矫凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角诧拷锟斤拷强锟斤拷锟皆碉拷循锟斤拷锟斤拷捉 slave 锟斤拷锟斤拷 */
$attempts = 0;
do {
/* check if slave has the table */
if ($res = $mysqli->query("SELECT id FROM test")) {
break;
} else if ($mysqli->errno) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
/* wait for slave to catch up */
usleep(200000);
} while ($attempts++ < 10);
/* Query has been run on a slave, result is in the cache */
assert($res);
var_dump($res->fetch_assoc());
/* Served from cache */
$res = $mysqli->query("SELECT id FROM test");
?>
锟斤拷锟斤拷姆锟斤拷锟剿碉拷锟斤拷锟斤拷使锟矫伙拷锟芥功锟杰★拷通锟斤拷 mysqlnd_ms_set_qos() 锟借定锟斤拷锟斤拷一锟斤拷锟皆凤拷锟斤拷锟斤拷锟斤拷锟金,诧拷锟斤拷锟斤拷锟斤拷使锟矫伙拷锟芥。然锟斤拷锟轿猴拷只锟斤拷锟侥诧拷锟斤拷锟斤拷锟结被锟斤拷锟斤拷 锟斤拷锟斤拷锟叫o拷锟斤拷锟绞憋拷锟酵拷锟� mysqlnd_ms_set_qos() 指锟斤拷锟斤拷
实锟绞碉拷 TTL 要锟斤拷通锟斤拷 mysqlnd_ms_set_qos() 锟借定锟斤拷要小锟斤拷锟借定值 锟斤拷锟斤拷锟斤拷锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷锟斤拷 TTL 锟较扣筹拷 slave 同锟斤拷锟接迟o拷锟斤拷锟节硷拷锟斤拷实锟绞碉拷 TTL锟斤拷锟节凤拷锟斤拷锟叫o拷 锟斤拷锟� slave 锟斤拷同锟斤拷锟接筹拷锟斤拷 10 锟诫,TTL 锟斤拷锟斤拷锟街碉拷锟� 60 锟诫,锟斤拷么锟斤拷锟斤拷锟� TTL 值锟斤拷 50 锟诫。 TTL 锟侥硷拷锟姐,锟斤拷锟斤拷每一锟斤拷锟斤拷锟斤拷锟斤拷目锟角讹拷锟斤拷锟侥★拷
Example #4 Read your writes and caching combined
<?php
$mysqli = new mysqli("myapp", "username", "password", "database");
if (!$mysqli)
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
if (!$mysqli->query("DROP TABLE IF EXISTS test") ||
!$mysqli->query("CREATE TABLE test(id INT)") ||
!$mysqli->query("INSERT INTO test(id) VALUES (1)"))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* Explicitly allow eventual consistency and caching (TTL <= 60 seconds) */
if (false == mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL, MYSQLND_MS_QOS_OPTION_CACHE, 60)) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
/* To make this example work, we must wait for a slave to catch up. Brute force style. */
$attempts = 0;
do {
/* check if slave has the table */
if ($res = $mysqli->query("SELECT id FROM test")) {
break;
} else if ($mysqli->errno) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
/* wait for slave to catch up */
usleep(200000);
} while ($attempts++ < 10);
assert($res);
/* Query has been run on a slave, result is in the cache */
var_dump($res->fetch_assoc());
/* Served from cache */
if (!($res = $mysqli->query("SELECT id FROM test")))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
var_dump($res->fetch_assoc());
/* Update on master */
if (!$mysqli->query("UPDATE test SET id = 2"))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* Read your writes */
if (false == mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_SESSION)) {
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
/* Fetch latest data */
if (!($res = $mysqli->query("SELECT id FROM test")))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
var_dump($res->fetch_assoc());
?>
锟斤拷锟斤拷锟借定锟斤拷锟斤拷锟斤拷时锟侥变,锟斤拷锟斤拷锟斤拷锟轿伙拷锟斤拷锟绞癸拷谩锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷锟斤拷员锟斤拷为 Session 一锟斤拷锟皆诧拷锟皆o拷 锟斤拷时锟斤拷锟芥将锟斤拷锟结被使锟矫o拷锟斤拷锟揭憋拷锟斤拷锟斤拷为锟斤拷锟铰碉拷锟斤拷锟捷斤拷锟斤拷锟�