Basic memcache operation instances and basic memcache operations in php
Basic memcache operation instance in php
<!DOCTYPE html>
Let's take a look at more specific instances.
<? Phpinclude ('inc/common. inc. php'); if (! Isset ($ city) |! Is_array ($ city) {exit;} // print_r ($ city); exit; $ mem = new Memcache (); $ mem-> connect ('localhost ', '20140901'); $ expires = 15*60; // check if cache exitsif ($ value = $ mem-> get ($ city ))! = FALSE) {echo "get key from memcache :". "<br/>"; // $ return = $ mem-> get ($ city); // echo json_encode ($ return );} // ifelse {$ resultJson = fetch_data (); echo count ($ resultJson ). "<br/>"; if (count ($ resultJson) = 1 | empty ($ resultJson) {// echo "get key from mysql: ". "<br/>"; $ query = "select * from pm25"; $ result = mysql_query ($ query); while ($ row = mysql_fetch_assoc ($ result )) {$ rows [] = $ row;} // Save the obtained value array to memca. Chefor ($ I = 0; $ I <count ($ rows); $ I ++) {$ k [$ I] = $ rows [$ I] ['city']; $ v [$ I] ['city'] = $ rows [$ I] ['city']; $ v [$ I] ['pm25'] = $ rows [$ I] ['pm25']; $ mem-> set ($ k [$ I], $ v [$ I], false, $ expires);} // $ return = $ mem-> get ($ city); // echo json_encode ($ return );} // ifelse {echo "get key from new_writed mysql :". "<br/>"; write_db ($ resultJson); $ query = "select * from pm25"; $ result = mysql_query ($ query ); while ($ row = mysql_fetch_assoc ($ re Sult) {$ rows [] = $ row;} // write memcachefor ($ I = 0; $ I <count ($ rows); $ I ++) {$ k [$ I] = $ rows [$ I] ['city']; $ v [$ I] ['city'] = $ rows [$ I] ['city']; $ v [$ I] ['pm25'] = $ rows [$ I] ['pm25']; $ mem-> 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.examp Le.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.txt foreach ($ getJson as $ key => $ row) {$ area [$ key] = $ row ['region']; $ pm2_5 [$ key] = $ row ['pm2 _ 5'];} array_multisort ($ area, SORT_ASC, $ pm2_5, SORT_ASC, $ getJson ); for ($ I = 0; $ I <count ($ getJson)-1; $ I ++) {if ($ getJson [$ I] ['pm2 _ 5'] = 0) $ count = 0; else $ count = 1; $ sum = $ getJson [$ I] ['pm2 _ 5']; for ($ j = $ I + 1; $ j <count ($ getJson ); $ j ++, $ I ++) {if (strcmp ($ getJson [$ j] ['region'], $ getJson [$ I] ['region']) = 0) {if ($ getJson [$ j] ['pm2 _ 5'] = 0) {continue;} else {$ count ++; $ sum + = $ getJson [$ j] ['pm2 _ 5']; $ pm2_5 = $ sum/$ count ;}} else {// insert into mysql $ result ['city'] = $ getJson [$ I] ['region']; $ result ['pm25'] = intval ($ pm2_5); $ query = "insert into pm25 (city, pm25) value S ('". $ result ['city']. "',". $ result ['pm25']. ")"; mysql_query ($ query); break ;}}return $ getJson;} // func write_db $ mem-> close ();?>
The above is all the content of this article. I hope you will like it.