Home > Backend Development > PHP Tutorial > php memcache and memcached module installation application_PHP tutorial

php memcache and memcached module installation application_PHP tutorial

WBOY
Release: 2016-07-20 11:09:56
Original
773 people have browsed it

The official homepage of memcache: php tutorial.net/package/memcache">http://pecl.php.net/package/memcache
The official homepage of memcached: http://pecl.php.net/package /memcached

The following is the process record of my installation of the Memcached version of the PHP module:

wget http://download.tangent.org/libmemcached-0.48.tar.gz
tar zxf libmemcached-0.48.tar.gz
cd libmemcached-0.48
./configure --prefix=/usr/local/libmemcached --with-memcached
make
make install

wget http://pecl.php.net/get/memcached-1.0.2.tgz
tar zxf memcached-1.0.2.tgz
cd memcached-1.0.2
/usr/local/webserver /php/bin/phpize
./configure --enable-memcached --with-php-config=/usr/local/webserver/php/bin/php-config --with-libmemcached-dir=/usr/ local/libmemcached
make
make install

Add
extension=memcached.so to php.ini
Complete

Another:
Install libmemcached If you only use ./configure, you may be prompted:
checking for memcached... no
configure: error: “could not find memcached binary”


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 fictitious problem. Assume that the initial value of counter is an integer. The increment method is not used, and each increment is completed 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);

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);


The two are almost identical to use.
Copy the code as follows:
$mem = new Memcache;
$mem->addServer($memcachehost, '11211');
$mem->addServer($memcachehost, '11212 ');
$mem->set('hx','9enjoy');
echo $mem->get('hx');

Copy the code as follows:
$md = new Memcached;
$servers = array(
array($memcachehost, '11211'),
array($memcachehost, '11212')
);
$ md->addServers($servers);
$md->set('hx','9enjoy');
echo $md->get('hx');

Memcached has many more methods than memcache, such as getMulti, getByKey, addServers, etc.
Memcached does not have the connect method of memcache, and it does not currently support long connections.
Memcached supports Binary Protocol, but memcache does not, which means memcached will have higher performance.
Memcache is implemented natively and supports the coexistence of OO and non-OO interfaces. memcached uses libmemcached and only supports OO interfaces.
More detailed differences: http://code.google.com/p/memcached/wiki/PHPClientComparison


The memcached server is a centralized caching system, and the distributed implementation method is determined by the client.
The memcached distribution algorithm generally has two options:
1. According to the result of hash(key), the remainder of the modular connection number determines which node to store, that is, hash(key)% sessions.size(), This algorithm is simple, fast and performs well. However, this algorithm has a shortcoming, that is, when memcached nodes are added or deleted, the original cached data will become invalid on a large scale, and the hit rate will be greatly affected. If there are many nodes and cached data, the cost of rebuilding the cache will be too high, so With the second algorithm.
2. Consistent Hashing, consistent hashing algorithm, its node search process is as follows:
First find the hash value of the memcached server (node), and configure it to the circle (continuum) of 0~232 superior. Then use the same method to find the hash value of the key storing the data and map it to the circle. It then searches clockwise starting from the location where the data is mapped, and saves the data to the first server found. If the server is still not found after exceeding 2 to the power of 32, it will be saved to the first memcached server.

memcache uses the first method without any configuration. To implement the first method, memcached seems to use (unconfirmed):
$md->setOption(Memcached::OPT_HASH, Memcached::HASH_CRC);

The second consistent hash Algorithm:

memcache is added in php.ini
Copy the code as follows:
Memcache.hash_strategy =consistent
Memcache.hash_function =crc32

memcached is added to the program (Unconfirmed)
Copy the code as follows:
$md->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$md->setOption(Memcached::OPT_HASH, Memcached: :HASH_CRC);
or
$mem->setOption(Memcached::OPT_DISTRIBUTION,Memcached::DISTRIBUTION_CONSISTENT);
$mem->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE,true);

Some reference documents:
memcached distribution test report (hash function selection in the case of consistent hashing):
http://www.iteye.com/topic/346682
php module memcache The difference with memcached: http://www.jb51.net/article/27366.htm
PHP module: Memcached > Memcache: http://www.jb51.net/article/27367.htm

20110509@@UPDATE:
If you install libmemcached, the following error message appears:
make[2]: *** [clients/ms_conn.o] Error 1
make[2]: Leaving directory `/www /soft/libmemcached-0.48'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/www/soft/libmemcached-0.48'
make: *** [all] Error 2

You can add --disable-64bit CFLAGS="-O3 -march=i686" when configure.
That is: ./configure --prefix=/usr/local /libmemcached --with-memcached --disable-64bit CFLAGS="-O3 -march=i686"


Analysis

memcache:http://cn2.php.net/ manual/en/book.memcache.php
memcached:http://cn2.php.net/manual/en/book.memcached.php
2.Memcache is implemented natively and supports both OO and non-OO Sockets coexist. Memcached uses libmemcached and only supports OO interface.
3. Another very commendable thing about memcached is that the flag is not set during operation, but has a unified setOption(). Memcached implements more of the memcached protocol.
4.memcached supports Binary Protocol, but memcache does not. This means memcached will have higher performance. However, memcached does not currently support long connections.

There is a table below to compare the php client extension memcache and memcached
http://code.google.com/p/memcached/wiki/PHPClientComparison

Another point is that everyone What is more concerning is the algorithm used. Everyone knows that the "consistent hash algorithm" is an algorithm that has less impact on the data stored on memcached when storage nodes are added or deleted. Then this algorithm can be used in both extension libraries of PHP, but the setting method is different.
Memcache
Modify php.ini and add:
[Memcache]
Memcache.allow_failover = 1
……
……
Memcache.hash_strategy =consistent
Memcache. hash_function =crc32
……
……
Or use the ini_set method in php:
Ini_set('memcache.hash_strategy','standard');
Ini_set('memcache.hash_function' ,'crc32');

Memcached
$mem = new memcached();
$mem->setOption(Memcached::OPT_DISTRIBUTION,Memcached::DISTRIBUTION_CONSISTENT);
$mem ->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE,true);


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444767.htmlTechArticlememcache’s official homepage: php tutorial.net/package/memcache">http://pecl.php.net /package/memcache The official homepage of memcached: http://pecl.php.net/package/memcached The following is how I installed Me...
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