Memcache Use Example | Php

Source: Internet
Author: User

First of all, to determine whether there is a key in the memcache, if there is a value from the memcache, if there is no corresponding key, from the page to crawl data, if the capture is successful, then into the DB, write memcache and read, if the crawl is not successful, then directly in the MySQL value, and put the value taken out into memcache.

<?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;
			The array of acquired values is stored in 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 foreach ($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 <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] [' 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 ();?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.