Basic memcache operations in php instance _ php instance

Source: Internet
Author: User
This article describes the basic operations and usage of Memcache in PHP. Share it with you for your reference. If you need it, study it. Basic memcache operation instance in php

memcache demo
 <?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);$array1 = array('name1' => 'jiajiam1', 'age1'=>12,'country'=>'china');$memcache->add("other",$array1,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 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 :"."
"; // $ Return = $ mem-> get ($ city); // echo json_encode ($ return) ;}// ifelse {$ resultJson = fetch_data (); echo count ($ resultJson )."
"; If (count ($ resultJson) = 1 | empty ($ resultJson) {// echo" get key from mysql :"."
"; $ Query =" select * from pm25 "; $ result = mysql_query ($ query); while ($ row = mysql_fetch_assoc ($ result )) {$ rows [] = $ row;} // Save the obtained value array to 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.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 Close ();?>

The above is all the content of this article. I hope you will like it.

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.