PHP Memcache Basic Operation example, Memcache basic operation
Memcache Basic operating example in PHP
<title>Memcache Demo</title>
<?php$server_ip = ' 127.0.0.1 '; $server _port = 11211; $memcache = new Memcache (); $memcache->connect ($server _ip,$ Server_port); $memcache->add ("name1", "user_name1", memcache_compressed,0); $memcache->add ("name2", "User_ Name2 ", memcache_compressed,0); $array 1 = Array (' name1 ' = ' jiajiam1 ', ' age1 ' =>12, ' country ' = ' China '); $ Memcache->add ("Other", $array 1,memcache_compressed,20), $memcache->set ("Name3", "User_name3", memcache_ compressed,0), $memcache->replace ("name1", "User_name_relpace", memcache_compressed,0); $memcache->replace (" 123 "," 12345 "); echo" name1: ". $memcache->get (" name1 ")."
"; $memcache->delete (" name1 "); echo" name1: ". $memcache->get (" name1 ")."
"; $array _get = Array (" Name1 "," name2 "," Name3 "), $result _get = $memcache->get ($array _get); foreach ($result _get as $ Key = $value) {echo ' $key:---> $value
";} foreach ($memcache->getstats () as $key = + $value) {echo $key:---> $value
";}; Echo
"; foreach ($memcache->getextendedstats () as $key = + $value) {echo" $key:---> $value
";} $memcache->close ();? >
Let's look at a more specific example.
<?phpinclude (' inc/common.inc.php '); if (! isset ($city) | |! Is_array ($city)) {exit;} Print_r ($city); exit, $mem = new Memcache (), $mem, connect (' localhost ', ' 11211 '); $expires =15*60;//check if cache Exitsif (($value = $mem, Get ($city))! = FALSE) {echo "Get key from Memcache:". "
";//$return = $mem->get ($city);//Echo Json_encode ($return);} IfElse {$resultJson =fetch_data (); Echo count ($resultJson). "
"; if (count ($resultJson) ==1 | | Empty ($resultJson)) {//value echo from MySQL ' Get key from MySQL: '. '
"; $query =" SELECT * from PM25 ", $result =mysql_query ($query), while ($row = Mysql_fetch_assoc ($result)) {$rows []= $row;} The array of fetched values is stored in memcachefor ($i =0; $i
Set ($k [$i], $v [$i], False, $expires);} $return = $mem->get ($city);//Echo Json_encode ($return);} Ifelse{echo "Get key from new_writed MySQL:". "
"; write_db ($resultJson); $query =" SELECT * from PM25 ", $result =mysql_query ($query); while ($row = MYSQL_FETCH_ASSOC ($ Result) {$rows []= $row;} Write Memcachefor ($i =0; $i
Set ($k [$i], $v [$i], False, $expires);} $return = $mem->get ($city);//Echo Json_encode ($return);} Else}//elseforeach ($city as $k = $v) {$return [$k]= $mem->get ($v);} echo Json_encode ($return), function Fetch_data () {$url = "http://www.example.com";//$url = ""; $data = Http_get ($url); $ Getjson = Json_decode ($data, True); return $getJson;} Func fetch_datafunction write_db ($getJson) {$sql = "DELETE from PM25"; mysql_query ($sql);//sort the Json.txtforeach ($ Getjson as $key + $row) {$area [$key] = $row [' area ']; $pm 2_5[$key]= $row [' pm2_5 '];} Array_multisort ($area, SORT_ASC, $pm 2_5,sort_asc, $getJson); for ($i =0; $i
<>
Close ();? >
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1000256.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000256.html techarticle PHP memcache Basic Operation example, Memcache Basic operation PHP memcache basic Operation Example! DOCTYPE htmlhtmlheadtitlememcache demo/titlemeta http-equiv= "Content-type" content= "text/...