This article illustrates the basic operation and usage of memcache in PHP. Share to everyone for your reference. Have a small partner in need to study it quickly.
PHP Memcache Basic Operation Example
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
Let's look at a more specific example.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
<?php include (' 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 exits if (($value = $mem-> Get ($city))!= FALSE) {echo ' Get key from Memcache: '. ' <br/> "; $return = $mem->get ($city); echo Json_encode ($return); }//if else {$resultJson =fetch_data (); Echo count ($resultJson). " <br/> "; if (count ($resultJson) ==1 | | empty ($resultJson)) {//from MySQL value 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 array of values memcache for ($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); }//if else{echo "Get key from New_writed MySQL: "." <br/> "; write_db ($resultJson); $query = "SELECT * from PM25"; $result =mysql_query ($query); while ($row = Mysql_fetch_assoc ($result)) {$rows []= $row;}//write memcache for ($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 }//else foreach ($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_data function write_db ($getJson) {$sql = "DELETE from PM25"; mysql_query ($sql);//sort the json.txt fo Reach ($getJson as $key => $row) {$area [$key] = $row [' area ']; $pm 2_5[$key]= $row [' pm2_5 '];} array_multisort ($area RT_ASC, $pm 2_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 ($getJson [$j] [' area '], $getJson [$i]] ==0) {if ($getJson [$j] [ Pm2_5 ']==0) {continue} else{$count + +; $sum + + = $getJson [$j] [' pm2_5 ']; $pm 2_5= $sum/$count;}} else{//insert into the MySQL $result [' City ']= $getJson [$i] [' area ']; $result [' Pm25 ']=intval ($pm 2_5); $query = "INSERT INTO PM25 (CITY,PM25) VALUES (' ". $result [' City ']." ', '. $result [' PM25 ']. "); mysql_query ($query); Break }} return $getJson; }//func write_db $MEM-> close ();?> |
The above is the entire contents of this article, I hope you can enjoy.