Home >Backend Development >PHP Tutorial >memcache PHP module Memcached has more features than Memcache

memcache PHP module Memcached has more features than Memcache

WBOY
WBOYOriginal
2016-07-29 08:45:32934browse

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 and add:
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 question, Assume that the initial value of counter is an integer, do not use the increment method, and complete increment by one each time through get/set.
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);
Since the two actions of get/set cannot be operated as an atom, when multiple processes process them at the same time, There is a possibility of loss, and what’s even more annoying is that you don’t know when it will be lost.
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 If you var_dump the value of $token, you will find that $token is actually a version number. If the version number of $token obtained through get does not correspond to the cas, it means that other operations have been updated. At this time, the cas operation will If it fails, it's up to you as to how to proceed.
Note: If you want to reproduce the conflict manually, 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 settings for the Memcached version module are 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 automatically supporting multiple servers through getByKey, setByKey, etc. I won’t go into details. Which extension to use is self-evident.
Supplement: http://code.google.com/p/memcached/wiki/PHPClientComparison

The above introduces the memcache PHP module. Memcached has more functions than Memcache, including memcache content. I hope it will be helpful to friends who are interested in PHP tutorials.

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