In the actual application, the result set of the database query is usually saved to memcached, the next access is obtained directly from the memcached, and the database query operation is not done.This can reduce the burden on the database to a large extent. SQL statements are typicallyMD5The value after () as a unique identifierKey. Below is an example of using memcached to cache a result set of a database query (this snippet immediately follows the example code):Copy the code code as follows: <?PHP
Initializing the memcache operation$sql= ' SELECT * from users ';$key=MD5($sql);//memcached Object Identifiersif( ! ($datas=$MC->get ($key)) ) {//If the cached data is not obtained in memcached, the recordset is obtained using a database query. Echo"N".Str_pad(' Read datas from MySQL. ', 60, ' _ '). " N;$conn=mysql_connect(' localhost ', ' test ', ' test ');mysql_select_db(' Test ');$result=mysql_query($sql); while($row=Mysql_fetch_object($result))$datas[] =$row;//saves the result set data obtained in the database to memcached for use on the next visit. $MC->add ($key,$datas);} Else {Echo"N".Str_pad(' Read datas from memcached. ', 60, ' _ '). " N;}Var_dump($datas);?>can see, after using memcached, can reduce database connection, query operation, database load down,The script is also running at a higher speed.
Memcache database Information Store to database reduce IO operations