PHP module: Memcached> Memcache

Source: Internet
Author: User
PHP module: Memcached & gt; Memcache PHP and Memcached are already well known standard. It seems too much "?" But some details are not clear to everyone. for example, in PECL, there are two Memcached modules, Memcache and Memcached, currently, most PHP environments use the Memcache version without d in the name. this version is released earlier and is a native version, the corresponding version of Memcached with d is based on libmemcached, so Memcached has more functions.

Install the PHP module of Memcached

Wget http://download.tangent.org/libmemcached-0.35.tar.gz
Tar zxf libmemcached-0.35.tar.gz
Cd libmemcached-0.35
./Configure
Make
Make install

Wget http://pecl.php.net/get/memcached-1.0.0.tgz
Tar zxf memcached-1.0.0.tgz
Cd memcached-1.0.0
Phpize
./Configure
Make
Make install

Open php. ini and add:

Extension = "memcached. so"

The installation is complete. you can run the following command to confirm the installation:

Php-m | grep mem

Demonstrate new features of Memcached

Assume that the counter initial value is an integer. if the increment method is not used, add one at a time through get/set.

In the Memcache version, we can only perform the following operations:

$ M = new Memcache ();
$ M-> addServer ('localhost', 11211 );
$ V = $ m-> get ('counter ');
$ M-> set ('counter ', $ v + 1 );

Because the get/set actions cannot be operated as one atom, when multiple processes are simultaneously processed, the possible loss may occur. What is even more annoying is that, you do not know when the loss occurs.

Let's take a look at how we did this in the Memcached version:

$ Md = new Memcached ();
$ Md-> addServer ('localhost', 11211 );
$ V = $ md-> get ('counter', null, $ token)
$ Md-> cas ($ token, 'counter', $ v + 1 );

Cas is a function provided by Memcached. to put it bluntly, it is an optimistic lock function. if you drop the $ token value var_dump, you will find that $ token is actually a version number, if the $ token version obtained through get does not match the cas version, it indicates that another operation has been updated. at this time, the cas operation will fail. As for how to continue the operation, it depends on yourself.

Note: If you want to manually reproduce the conflict, you can sleep between get and cas for several seconds, copy the two scripts, and execute them successively.

By the way, the hash settings of the recommended Memcached version module are as follows:

$ Md-> setOption (Memcached: OPT_DISTRIBUTION, Memcached: DISTRIBUTION_CONSISTENT );
$ Md-> setOption (Memcached: OPT_HASH, Memcached: HASH_CRC );

Summary

Memcached also has many features not available in Memcache. for example, if getByKey and setByKey are used to automatically support multiple servers, we will not go into details about which extension should be used.

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.