1. Query multiple rows of records, with SQL MD5 value of key, cache array (the method that the person thinks is best)
$mem =NewMemcache ();$mem->connect (' 127.0.0.1 ', 11211);$id = (int)$_get[' ID '];$sql = "SELECT * FROM Test WHERE id= '$id ' ";$key =MD5 ($sql);//Whether the database query has been cached to the memcahced serverif (! ($datas =$mem->get ($key))){echo ' mysql<br/> ';//If you don't get the data in memcached, even MySQL gets$conn =mysql_connect (' localhost ', ' root ', ' 123456 ');mysql_select_db (' Test ');$result =mysql_query ( $sql ); while ( $row = mysql_ FETCH_ASSOC ( $result $datas [] = $row ;} // $mem->add ( Span style= "color: #800080;" > $key, $datas else{echo "memcache <br/> ' ;} print_r ( $datas);
2. Query a single-line record, cache the row record, with the ID value of key (also available as the MD5 SQL statement)
$rangeid =Rand (600,1276);$rangeid = ' 1237 ';$mem =NewMemcache;$mem->connect (' 127.0.0.1 ', 11211);if (($com =$mem->get ($rangeid)) = = =False) {echo ' from mysql<br/> ';$conn =mysql_connect (' localhost ', ' root ', ' 123456 ');$sql = ' Use Dedecms ';mysql_query ($sql,$conn);$sql = ' Set names UTF8 ';mysql_query ($sql,$conn);$sql = ' Select Aid,actors from Dede_addonmovie where aid= '.$rangeid;$rs =mysql_query ( $sql, $ Conn); $com = mysql_fetch_assoc ( $rs ); $mem->add ( $rangeid, $com, false, 60else {echo "from Memcache <br/> ' ;} header (' Content-type:text/html;charset=utf8; ') ); print_r ( $com);
You can also connect memcache in a different way
$rangeid =Rand (600,1276);$mconn = Memcache_connect (' localhost ', 11211);if (($com = Memcache_get ($mconn,$rangeid)) = = =False) {$conn =mysql_connect (' localhost ', ' root ', ' 123456 ');$sql = ' Use Dedecms ';mysql_query ($sql,$conn);$sql = ' Set names UTF8 ';mysql_query ($sql,$conn);$sql = ' Select Aid,actors from Dede_addonmovie where aid= '.$rangeid $rs = mysql_query ( $sql , $conn ); $com = mysql_fetch_assoc ( $rs $ Mconn, $rangeid, $com, mt_rand (40,120else{echo ' from cache ' print_r ( $com);
PHP using memcached Cache Summary