PHP module memcached features more than memcache_php skills

Source: Internet
Author: User
For example, there are two memcached modules in Pecl, Memcache and memcached, and most PHP environments use a memcache version with no d in the name, which is released earlier and is a native version, The corresponding memcached version with D is based on the libmemcached, so the memcached version of the function is more complete.

Installing the memcached version of the PHP module

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 Plus:

Extension = "memcached.so"

This is the end of the installation, and you can confirm it by following these commands:

php-m | grep mem

demonstrates new features of the memcached version

To make up a problem, assume that the counter initial value is an integer and do not use the Increment method to complete each addition by Get/set.

In the Memcache version, we can only do this in the following way:

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

Since get/set these two actions cannot operate as an atom, there is the possibility of loss when multiple processes are processed at the same time, and what is even more annoying is that you do not know when a loss occurs.

And look at the memcached version of how we did it:

$MD = new Memcached ();
$MD->addserver (' localhost ', 11211);
$v = $md->get (' counter ', NULL, $token)
$MD->cas ($token, ' counter ', $v + 1);

CAS is a memcached version of the function, the white is an optimistic lock function, if you $token the value of Var_dump out, you will find that $token is actually a version number, if you get the $token version number in the CAS does not correspond, It means that there are other actions that have been updated, at which point the CAS operation will fail, and how to proceed is up to you.

Note: If you want to reproduce the conflict manually, you can sleep a few seconds between get and CAs and copy two scripts to execute.

Incidentally, the recommended memcached version of the module has the following hash settings:

$MD->setoption (memcached::opt_distribution, Memcached::D istribution_consistent);
$MD->setoption (Memcached::opt_hash, MEMCACHED::HASH_CRC);

Summary

Memcached version There are many memcache functions, such as through the Getbykey, Setbykey and other automatic support for multiple servers, do not repeat, which extension has been self-evident.

Add:Http://code.google.com/p/memcached/wiki/PHPClientComparison

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.