Memcached Notes and summaries (4) Use of memcache extensions

Source: Internet
Author: User

Test in Wamp Environment: Wampserver 2.2 (Windows 7 + Apache 2.2.21 + PHP 5.3.10 + memcache 3.0.8 + Memcached 1.4.24)

To open the Memcached service locally:

Simple example:

<? PHP // Create a Memcache object $MC New Memcache (); // Connecting memcached servers $MC->connect (' 127.0.0.1 ', 11211); // Storing Data $MC->set (' name ', ' Dee ', 0, 20); // 0: Not applicable zlib compression 20: Cache 20 sec//Get Data $name $MC->get (' name '); Var_dump ($name);

Output:

Use the Telnet client to connect to the Memcached server and get the cached data:

The value of key name is not obtained after 20 seconds.

Simple Example 2:

<?PHP//Create a Memcache object$MC=NewMemcache ();//Connecting memcached servers$MC->connect (' 127.0.0.1 ', 11211);//Storing Data$MC->set (' name ', ' Dee ', 0, 20);//0: Not applicable zlib compression 20: Cache 20 sec//Get Data$name=$MC->get (' name ');Var_dump($name);//Delete Data$MC->delete (' name ');$name=$MC->get (' name ');Var_dump($name);//force flush of all caches, i.e. empty memcached server$MC-Flush();//disconnecting from the memcached server$MC->close ();

Output:

Simple Example 3:

memcache::getstats Gets the server statistics, returns a server statistic in the form of an associative array, the value of which is the value of the statistic, which can be used: Reset,malloc,maps,cachedump,slabs,items , sizes, for example:

<? PHP // Create a Memcache object $MC New Memcache (); // Connecting memcached servers $MC->connect (' 127.0.0.1 ', 11211); // get statistics for a server $serverStats $MC->getstats (' slabs '); Var_dump ($serverStats);

Output:

Memcached uses the Slab memory allocation algorithm when storing data, which reduces the generation of memory fragmentation and increases memory utilization.

Another example:

<? PHP // Create a Memcache object $MC New Memcache (); // Connecting memcached servers $MC->connect (' 127.0.0.1 ', 11211); // get statistics for a server $serverStats $MC->getstats (' Items '); Var_dump ($serverStats);

Output:

Another example:

<? PHP // Create a Memcache object $MC New Memcache (); // Connecting memcached servers $MC->connect (' 127.0.0.1 ', 11211); // get statistics for a server $serverStats $MC->getstats (' sizes '); Var_dump ($serverStats);

Output:

Simple Example 4:

Memcache::getserverstatus, get a server online (1)/offline status (0)

<? PHP // Create a Memcache object $MC New Memcache (); // Connecting memcached servers $MC->connect (' 127.0.0.1 ', 11211); // get statistics for a server $serverStatus $MC->getserverstatus (' 127.0.0.1 ', 11211); Var_dump ($serverStatus);

Output: int 1

Description: The first parameter is the server domain name or IP, the second parameter is the port, the default 11211

Simple Example 5:

By saving the results from the database query to Memcached, the next access is taken directly from the Memcached, thereby reducing the burden on the database

<?PHP//Create a Memcache object$MC=NewMemcache ();//Connecting memcached servers$MC->connect (' 127.0.0.1 ', 11211);$id= (int)$_get[' ID '];$sql= ' SELECT * from message where id = '.$id;$key=MD5($sql);//determine if the database query results have been cached in the memcached server$datas=$MC->get ($key);if(!$datas) {    //no cache data obtained, query directly from MySQL    $conn=mysql_connect(' 127.0.0.1 ', ' root ', '); mysql_select_db(' msg '); $result=mysql_query($sql);  while($row=Mysql_fetch_object($result)){        $datas[] =$row; }    //Save the result set to memcached    $MC->set ($key,$datas);}Var_dump($datas);

Output:

Note: The SQL statement to be queried is encrypted into a unique key by MD5 (), using the key to go to the Memcached query, if there is already the cache, then return directly, if not, then go to the MySQL database query and then return the results and cache to the Memcached server.

List all keys in the Telnet client:

#List all keysStats items//this one's an order .STATItems:4: Number1STATItems:4:age 347STATitems:4:evicted 0STATItems:4:evicted_nonzero 0STATItems:4:evicted_time 0STATItems:4:outofmemory 0STATItems:4:tailrepairs 0STATItems:4:reclaimed 0STATitems:4:expired_unfetched 0STATitems:4:evicted_unfetched 0STATItems:4:crawler_reclaimed 0STATitems:4:crawler_items_checked 0STATitems:4:lrutail_reflocked 0END#get key with item ID, in this case 4#the 2nd parameter is the length listed, and 0 represents all listedStats Cachedump 4 0//this one's an order .ITEM 378adbfe4fe7b6a2bbcad92d670db3c9 [B; 1443880812S]END#get the key value by GetGet 378ADBFE4FE7B6A2BBCAD92D670DB3C9//this one's an order .VALUE 378ADBFE4FE7B6A2BBCAD92D670DB3C9 1 91a: 1:{i:0;o:8: "StdClass": 3:{s:2: "id"; s:1: "1"; s:7: "Content"; s:5: "Hello"; s:4: "Flag"; s:1: "3";}}END

, PHP's memcache extensions are automatically serialize and unserialize, so you can store arrays or objects directly.

Reference: "Memcache View list all key methods"

Memcached Notes and summaries (4) Use of memcache extensions

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.