Simple MemcachePHP tutorial

Source: Internet
Author: User
Recently, projects have a large volume of access traffic and frequent database reading. The use of cache is an urgent issue. After various traversal on the internet, memcache is used as the cache. Since there are very few tutorials on memcache on the internet, I will sort out the collected materials and usage experiences here. About Memcache

Recently, projects have a large volume of access traffic and frequent database reading. The use of cache is an urgent issue. After various traversal on the internet, memcache is used as the cache. Since there are very few tutorials on memcache on the internet, I will sort out the collected materials and usage experiences here. About Memcache

Recently, projects have a large volume of access traffic and frequent database reading. The use of cache is an urgent issue.

After various traversal on the internet, memcache is used as the cache.

Since there are very few tutorials on memcache on the internet, I will sort out the collected materials and usage experiences here.


About Memcache

I simply understand it as a database that uses memory storage... Other google go...


Install Memcache

You need to install two things: one is the memcached main program and the other is the PHP extension.

The latest Memcache is 1.4.15, specific can see the official website: http://memcached.org/

Installation Method:

Https://code.google.com/p/memcached/wiki/NewInstallFromSource

Http://www.ccvita.com/259.html

Win: http://www.ccvita.com/258.html


Use Memcache for PHP

 Connect ("192.168.0.100", 12000); // save data $ mem-> set ('key1', 'this is first value', 0, 3600 ); $ val = $ mem-> get ('key1'); echo "Get key1 value :". $ val."
"; // Save the array $ arr = array ('aaa', 'bbb ', 'ccc', 'ddd '); $ mem-> set ('key2 ', $ arr, 0, 60); $ val2 = $ mem-> get ('key2'); echo "Get key2 value:"; print_r ($ val2); echo"
"; // Delete data $ mem-> delete ('key1'); $ val = $ mem-> get ('key1'); echo" Get key1 value :". $ val."
"; // Clear all data $ mem-> flush (); $ val2 = $ mem-> get ('key2'); echo" Get key2 value :"; print_r ($ val2); echo"
"; // Close the connection $ mem-> close ();?>

The output result of the browser is:

Get key1 value: This is first value

Get key2 value: Array ([0] => aaa [1] => bbb [2] => ccc [3] => ddd)

Get key1 value:

Get key2 value:


Code Analysis

$mem = new Memcache;

Initialize a Memcache object.

$mem->connect("192.168.0.100", 12000);

Connect to the Memcache server. The first parameter is the Server IP address or host name, and the second parameter is the open port of Memcache.

Like Mysql, the persistent connection method of pconnect ($ host, $ port) is also available. This method cannot be closed by close.

$mem->set('key1', 'This is first value', 0, 3600);

Save a data to the Memcache server. The first parameter is the data key used to locate a data index. The second parameter is the data content to be saved. Here is a string, the third parameter is a tag, which is usually set to 0 or MEMCACHE_COMPRESSED (Compressed). The fourth parameter is the data validity period, that is, the data is valid during this time, the data will be cleared by the Memcache server in seconds. If it is set to 0, it will always be valid. We have set 3600 here, which is an hour.

At the same time, I also found the add () and replace () methods, one plug data and one modify data. set is their integrated version.

$val = $mem->get('key1');

Get a piece of data from the Memcache server. It has only one parameter, that is, the key to get the data. Here is the key1 set in the previous step.

$arr = array('aaa', 'bbb', 'ccc', 'ddd');$mem->set('key2', $arr, 0, 60); 

Similarly, Memcache can save arrays.

$mem->delete('key1');

Similarly, this key is used to delete a single item.

$mem->flush();

Use caution when clearing data on the Memcache server, if there is someone else's cache on the server.

For more methods of Memcache, see this: http://www.php.net/memcache

Let's take a look at this article for more distributed applications. Http://www.ccvita.com/395.html


Memadmin management tool

We recommend a memadmin tool.

This is a program for Memcache management. It has powerful functions and beautiful interfaces, which can improve the efficiency of development and debugging. You can understand the specific usage at a glance and will not introduce it.

Original article address: simple Memcache PHP tutorial. Thank you for sharing it with me.

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.