PHP module Memcached has more features than Memcache_PHP tutorial

WBOY
Release: 2016-07-21 15:27:56
Original
741 people have browsed it

For example, there are two Memcached modules in PECL, Memcache and Memcached. Currently, most PHP environments use the Memcache version without d in the name. This version was released earlier and is a native version. The Memcached version with d is based on libmemcached, so the Memcached version has more complete functions.

Install 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"

The installation is complete. You can confirm it with the following command:

php -m | grep mem

Demonstrate the new features of the Memcached version

First make up a problem, assuming that the initial value of counter is an integer, do not use the increment method, and complete each increment through get/set one.

In the Memcache version, we can only proceed as follows:

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

Due to get/ The two actions of set cannot be operated as an atom, so when multiple processes process it at the same time, there is a possibility of loss. What is even more annoying is that you don't know when the loss occurs.

Let’s look at how we do it 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 in the Memcached version. To put it bluntly, it is an optimistic locking function. If you var_dump the value of $token, you will find that $token is actually a version number. If the $token version number obtained through get is in If it does not correspond to the cas operation, it means that other operations have been updated. At this time, the cas operation will fail. As for how to continue the operation, it is up to you.

Note: If you want to manually reproduce the conflict situation, you can sleep for a few seconds between get and cas, copy the two scripts, and execute them one after another.

By the way, the recommended hash setting for the Memcached version module is as follows:

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

Summary

The Memcached version also has many functions that Memcache does not have, such as through getByKey, setByKey I won’t go into details when multiple servers are automatically supported. It’s self-evident which extension to use.

Supplementary: http://code.google.com/p/memcached/wiki/PHPClientComparison

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323692.htmlTechArticleFor example, there are two Memcached modules in PECL, Memcache and Memcached. Currently, most PHP environments use Memcache version without d in the name, this version was released earlier, it is...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!