Note: �汾���������Ժ�״̬
���Դ� concepts section ��ȡ��������ݡ�
���ݿ�Ⱥ������趨��ͬ��һ���Լ��𡣴� 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 ʧЧ���Կ��ƵĻ�������������ݿ�ڵ��ȡ���ݡ����ݿ�ڵ�ͻ��涼���� ����һ���Բ��ԣ����ǿ��ܱ���IJ������µ����ݡ�
ͨ�����ػ���������ݿ������������Ч���������ܣ��������ݿ�ѹ���� ��������ͻ��˱ȴ������������Ŀ�Ŀͻ��˸�Ƶ��ʹ��������ô���ݿ�ķ��ʾͱ����������� �Ӷ��������ݿ�ѹ�������ң����ڱ��ػ�����ٶȿ������ݿ��ѯ����ô����������ܾͱ�������
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));
/* ȷ����������һ���ԣ������趨������Ч�� (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));
}
/* Ϊ���÷������������Dz���ǿ���Ե�ѭ���� 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() �趨����һ���Է��������𣬲�������ʹ�û��档Ȼ���κ�ֻ���IJ������ᱻ���� �����У����ʱ��ͨ�� mysqlnd_ms_set_qos() ָ����
ʵ�ʵ� TTL Ҫ��ͨ�� mysqlnd_ms_set_qos() �趨��ҪС���趨ֵ ���������ֵ��������� TTL �Ͽ۳� slave ͬ���ӳ٣����ڼ���ʵ�ʵ� TTL���ڷ����У� ��� 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 һ���Բ��ԣ� ��ʱ���潫���ᱻʹ�ã����ұ�����Ϊ���µ����ݽ����